Need best approach to use a class inside other class
-
I have a base class say “ClassBase” and I have derived two classes “Classdrv1” & “Classdrv2” from base class as shown below. Now I am not sure how to use class “Classdrv2” in “Classdrv1”. For this implementation there should not be any code change in base class. What is the best approach for the same? Class ClassBase { } class Classdrv1 : public ClassBase { // need to use classdrv2 here, how? } class Classdrv2 : public ClassBase { } Thanks, Nandu
-
I have a base class say “ClassBase” and I have derived two classes “Classdrv1” & “Classdrv2” from base class as shown below. Now I am not sure how to use class “Classdrv2” in “Classdrv1”. For this implementation there should not be any code change in base class. What is the best approach for the same? Class ClassBase { } class Classdrv1 : public ClassBase { // need to use classdrv2 here, how? } class Classdrv2 : public ClassBase { } Thanks, Nandu
You may do, for instance:
class Base
{
//...
};
class Der2;
class Der1: public Base
{
Der2 * pd2;
//...
};
class Der2: public Base
{
//..
};:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You may do, for instance:
class Base
{
//...
};
class Der2;
class Der1: public Base
{
Der2 * pd2;
//...
};
class Der2: public Base
{
//..
};:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have a base class say “ClassBase” and I have derived two classes “Classdrv1” & “Classdrv2” from base class as shown below. Now I am not sure how to use class “Classdrv2” in “Classdrv1”. For this implementation there should not be any code change in base class. What is the best approach for the same? Class ClassBase { } class Classdrv1 : public ClassBase { // need to use classdrv2 here, how? } class Classdrv2 : public ClassBase { } Thanks, Nandu
Just how do you plan to use it? You do realize that to use the class, unless you are talking about a static (class) function, you need an object of the class. So how are you getting this object & how long are you keeping it for? How does it relate to the rest of the program? An object of Classdrv2 of its own for the entire lifetime of a Classdrv1 object? An object of Classdrv2 just for use during the execution of a member function of Classdrv1? An object of Classdrv2 provided by a caller for manipulation by a member function of Classdrv1? There are lots of possibilities. Which is right for your situation depends on what you are trying to achieve. If you explained more, you might get more focused answers.
Please do not read this signature.