Aggregation order call

S

sreeni

I have a similar class hierarchy

class Base
{
public:

virtual get_method1();

virtual get_method2();
}

class derived1 :public base
{
public:
friend class derived2;
virtual get_method1();
virtual get_method2();
void method(); // internally calls get_method1();get_method2()
}

class derived3:public base
{
public:
virtual get_method1();
virtual get_method2();
}



class derived2 : public derived3
{
public:

virtual get_method();
virtual get_method2();

private :
derived1* p;
}

i have to handle two scenarios

i ) i need to call derived1 function method from derived2 through
pointer p this can be achieved with current setup

p->method() which internally would call get_method1() and
get_method2() of derived1

ii) i need to call derived1 function method from derived2 through
pointer p

but method() of derived1 should call get_method1() and get_method2()
of derived2 obj

Please tell if its possible to do the same.
 
L

Leandro Melo

I have a similar class hierarchy

class Base
{
public:

virtual get_method1();

virtual get_method2();

}

class derived1 :public base
{
public:
friend class derived2;
virtual get_method1();
virtual get_method2();
void method(); // internally calls get_method1();get_method2()

}

class derived3:public base
{
public:
virtual get_method1();
virtual get_method2();

}

class derived2 : public derived3
{
public:

virtual get_method();
virtual get_method2();

private :
derived1* p;

}

i have to handle two scenarios

i ) i need to call derived1 function method from derived2 through
pointer p this can be achieved with current setup

p->method() which internally would call get_method1() and
get_method2() of derived1


Not necessarily, it dependents on what the pointer is actually
pointing to. For instance, it can point to a derived class of
derived1. Also, why is derived2 friend of derived1?
ii) i need to call derived1 function method from derived2 through
pointer p

but method() of derived1 should call get_method1() and get_method2()
of derived2 obj

Please tell if its possible to do the same.

Your overall design looks weird to me... Since derived2 is not part of
derived1's hierarchy, one possibility would be to use a base pointer
and redesign derived1's interface to call the methods based on the
pointer.
 
M

mlimber

I have a similar class hierarchy

 class Base

Rename to "base" to make this compile.
{
public:

virtual  get_method1();

virtual  get_method2();

}

You forgot the return type, the virtual destructor (see <http://
www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7>), and
a semicolon after the class. And likewise throughout.
class derived1 :public base
{
public:
friend class derived2;

This friend is unnecessary. All functions are public. Cf. <http://
www.parashift.com/c++-faq-lite/friends.html#faq-14.1>.
virtual  get_method1();
virtual  get_method2();
void method(); // internally calls get_method1();get_method2()

I presume you meant that it calls them without qualification, e.g.,

void method()
{
get_method1();
get_method2();
}
}

class derived3:public base
{
public:
virtual  get_method1();
virtual  get_method2();

}

class derived2 : public derived3
{
public:

virtual  get_method();

I presume you meant "get_method1();"
virtual  get_method2();

 private :
 derived1* p;

}

i have to handle two scenarios

i ) i need to call derived1 function method from derived2 through
pointer p this can be achieved  with current setup

p->method() which internally would call get_method1() and
get_method2() of derived1

Alternately, you could call p->get_method1() and p->get_method2()
directly since they're public.
ii) i need to call derived1 function method from derived2 through
pointer p

but method() of derived1 should call get_method1() and get_method2()
of derived2 obj

Not as written. Your derived1 has no access to an *instance* of
derived2 whatsoever (the friend statement does nothing in this
respect).

You could rewrite derived1's method() function something like this:

class derived1 : public base
{
// ...

void method( base* b )
{
b->get_method1();
b->get_method2();
}
};

Then when you want derived1's virtual functions, send in a pointer to
a derived1 instance, and likewise with a derived2 instance:

void derived2::my_func()
{
p->method( p ); // calls derived1 functions
p->method( this ); // calls derived2 functions
}

(You could also make derived1::method() static in my implementation
since it doesn't access local variables, but in that case, it should
probably be pulled out of the class altogether.)

However, I suspect that this code displays a misunderstanding of
polymorphism and how classes are supposed to work. See these FAQs on
inheritance:

http://www.parashift.com/c++-faq-lite/virtual-functions.html
http://www.parashift.com/c++-faq-lite/proper-inheritance.html
http://www.parashift.com/c++-faq-lite/abcs.html

and/or a good C++ book (_Accelerated C++_ by Koenig and Moo, for
instance).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top