Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..

This is what I want to do :
1. Pass an object pointer of a class (say X) to the constructor of another class(say Y).

Now my question is :

Can I access the public members of class X in member functions of class Y ? If possible then I want to know how..as it's giving me some runtime failures.


Thankss in advance..!!


EDIT: Update to original question from the OP. [enhzflep]


This is what i wish to do :

C++
Class X
{
	public:
		int ver;
}

class Y
{
    y(X *obj)
    {
    }
	 
    void someMethod
    {
        cout<< obj.ver;
    }
}

void main()
{
  y obj1=new y();
  y.someMethod;
}


Can this be done.??
Posted
Updated 21-May-13 1:52am
v2
Comments
Prasaad SJ 21-May-13 5:37am    
Can't you access the public members of X as normal function arguments.
For example Y(X o){int i=o.i;}
You can use X as this way.
enhzflep 21-May-13 7:57am    
Yes, this functionality can be achieved. Though you need to make a few changes.
1) You'll need ; characters after the closing brace for the class definitions.

2) You're passing a pointer to an object of class X, into the constructor for class Y. You then go on to try to print the 'ver' variable of this instance of X. Only problem is, you threw away the copy you got in the constructor....
Hint: Add a variable to hold the pointer given to you in the constructor. hintB: don't forget to change from . notation to -> notation when you use the pointer.

Content moved to original question.
 
Share this answer
 
v2
Yes, so long as class Y has a member variable of type Class X.

C++
#include <cstdio>

class class1
{
public:
    bool initialized;
    bool otherFuncUsed;
    class1()
    {
        initialized = true;
        otherFuncUsed = false;
    }
    void sampleFunc()
    {
        otherFuncUsed = true;
    }
};


class class2
{
public:
    class1 memberClass;
    class2()
    {
        printf("memberClass.otherFuncUsed = %d\n", memberClass.otherFuncUsed);
        memberClass.sampleFunc();
        printf("memberClass.otherFuncUsed = %d\n", memberClass.otherFuncUsed);
    }
};

int main()
{
    class2 sampleVar;
}
 
Share this answer
 
Yes, you can.
You can access the public members of the object via the -> operator, of course.
I can't know why are you getting runtime failures, however I might suspect you are calling methods (or accessing data) of an invalid pointer (e.g. is the passed object still alive - has not be released - while you are accessing it?).


[Update]
C++
#include <iostream>
using namespace std;
class X
{
public:
    int ver;
    X(int v):ver(v){}
};

class Y
{
  X * px;
public:
  Y(X * pObj):px(pObj){}
  void showXVer(){ cout << "x version: " << px->ver << endl;}
};

int main()
{
    X x(42);
    Y y(&x);
    y.showXVer();
}


[/Update]
 
Share this answer
 
v4
Comments
AsthaS 21-May-13 5:39am    
Thanks for the prompt reply...actually I am passing the object pointer to the constructor, and accessing the public members in the other member functions of the class...my doubt is can this be done..?? when i am accessing public members within the constructor the code is working fine..but when i copy the same line in the other member function its giving me "run failed" message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900