|
-
March 26th, 2010, 12:31 AM
#1
Polymorphism
Hello.
I have 3 classes. B and C inherit from A. I want that class B will be able to call functions of C, so I tried to do this:
Code:
class A
{
public:
A(int b=0) {a=b;}
protected:
void Print() {cout<<"Hello world"<<endl;}
private:
int a;
};
class B: public A
{
public:
B(int b=0): A(b){}
void Do(A& a) {a.Print();} //error
};
class C: public A
{
C(int b=0): A(b){}
int c;
};
but i get an error:
error: `virtual void A::Print()' is protected.
How can I do it without using "friend" (and without making Print public) ?
Last edited by ovidiucucu; March 26th, 2010 at 01:16 AM.
Reason: added [CODE] tags
-
March 26th, 2010, 01:15 AM
#2
Re: Polymorphism
Why does A not have any virtual functions, not even a virtual destructor? It looks like you cannot use inheritance based polymorphism unless you change A.
Other than that... what does B have to do with C?
-
March 26th, 2010, 01:53 AM
#3
Re: Polymorphism
 Originally Posted by omerd
Hello.
I have 3 classes. B and C inherit from A. I want that class B will be able to call functions of C, so I tried to do this:
In that case B and C must be related by inheritance like for example,
class A {};
class C : public A {};
class B : public C {};
Both B and C inherits A, C directly and B indirectly via C.
This is also possible,
class A {};
class C : public A {};
class B : public A, public C {};
It's essentially as before apart from that if A carries implementation it's now an example of the (in)famous so called diamond inheritance. You can resolve the ambiguity by the use of virtual inheritance like this,
class A {};
class C : virtual public A {};
class B : virtual public A, public C {};
If the purpose if inheriting C in B was only to get at the implementation of C then you can inherit C privately, like
class A {};
class C : virtual public A {};
class B : virtual public A, private C {};
Now B isn't a C class as it was before, Just an A class.
As you can see there are many options and ample opportunity to make a mess out of a design in C++.
Last edited by nuzzle; March 26th, 2010 at 04:12 AM.
-
March 26th, 2010, 01:57 AM
#4
Re: Polymorphism
error: `virtual void A::Print()' is protected.
This error shows the code you shown is incorrect. Please post the actual snippet, otherwise we can't help you too much.
-
March 26th, 2010, 07:18 AM
#5
Re: Polymorphism
Your thread title, code and description don't have much in common.
There's no polymorphism in the code you provided.
You're not trying to call in functions in C and the code you provided doesn't produce the error you report.
What are you really trying to do?
-
March 26th, 2010, 07:57 AM
#6
Re: Polymorphism
maybe
I changed the code a little bit. Now it is more specific, but I also tried to keep it simple.
Actually, B and C manage a list of objects of type D, and in some cases B need to get C's D objects. B and C create objects of D in different ways.
If it's help you, C holds a list of object of type B.
Code:
class D
{
public:
D(string s): name(s)
{another_data =-1;}
D(string s,int data): name(s),another_data(data)
{}
D(const D& d)
{/*code*/}
string GetName() {return name;}
int GetData() {return another_data;}
private:
string name;
int another_data;
};
class A
{
public:
A(int b=0) {a=b;}
virtual ~A()
{
//code;
}
protected:
virtual D* FindD(string)=0;
private:
int a;
};
class B: public A
{
friend class A;
public:
B(int b=0): A(b){}
D* FindD(string name)
{
//code that find a specific D
//and return it
}
void Do(A& a, string name)
{
a.FindD(name); //error: `virtual D A::FindD(std::string)' is protected|
}
private:
list<D> names;
};
class C: public A
{
C(int b=0): A(b){}
D* FindD(string name)
{
//code that find a specific D
//and return it
}
list<D> names;
};
Last edited by omerd; March 26th, 2010 at 08:01 AM.
-
March 26th, 2010, 08:09 AM
#7
Re: Polymorphism
Why not put the list in A?
-
March 26th, 2010, 08:26 AM
#8
Re: Polymorphism
you right, but it doesn't help. class B need to call "FindD" on objects of type C (which is the argument in function "Do")
-
March 26th, 2010, 08:43 AM
#9
Re: Polymorphism
 Originally Posted by omerd
you right, but it doesn't help. class B need to call "FindD" on objects of type C (which is the argument in function "Do")
Okay, so class B would need an instance of class C to do that. What's the problem? Just call FindD from your B object using your instance of C.
From an inheritance POV, if B and C both have a list of D, that list should be in a common base class.
-
March 26th, 2010, 09:10 AM
#10
Re: Polymorphism
 Originally Posted by GCDEF
Okay, so class B would need an instance of class C to do that. What's Just call FindD from your B object using your instance of C.
that what's I did. B call "FindD" on C's object, and I get an error.
Last edited by omerd; March 26th, 2010 at 09:12 AM.
-
March 26th, 2010, 09:18 AM
#11
Re: Polymorphism
 Originally Posted by omerd
that what's I did. B call "FindD" on C's object, and I get an error.
Show the smallest and simplest compilable program that demonstrates that error.
-
March 26th, 2010, 09:34 AM
#12
Re: Polymorphism
 Originally Posted by omerd
that what's I did. B call "FindD" on C's object, and I get an error.
Don't you think explaining the error a bit may help us help you?
-
March 26th, 2010, 09:48 AM
#13
Re: Polymorphism
 Originally Posted by GCDEF
Don't you think explaining the error a bit may help us help you?
I wrote the error above:
error: `virtual void A::Print()' is protected.
 Originally Posted by laserlight
Show the smallest and simplest compilable program that demonstrates that error.
I will edit this message in a few hours with the code.
Last edited by omerd; March 26th, 2010 at 10:30 AM.
-
March 26th, 2010, 10:40 AM
#14
Re: Polymorphism
So either make FindD public in C, or else declare that B is a friend of C. I'm not sure which is more appropriate in this specific situation.
-
March 26th, 2010, 10:58 AM
#15
Re: Polymorphism
But B and C are both derived from A. Both classes contain a list of D. Why not move both the list and the FindD function into A?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|