will this work?

P

puzzlecracker

class A{};

class B:public A{};

class C: publoc B{};


void foo(const A& a){}


int main (int argc, char * [] argv){

C c;
A & a= c;

foo(a);
foo(c);

}

I am curious how transitivity is usually used in C++.

Thanks
 
B

Ben Pope

puzzlecracker wrote:

No, it won't work.
class A{};

class B:public A{};

class C: publoc B{};

// I suspect that fails to compile.
void foo(const A& a){}


int main (int argc, char * [] argv){

// This also won't compile, try
int main() {
C c;
A & a= c;

foo(a);
foo(c);

}

With the fixes, it compiles. Depends what you mean by "works".
I am curious how transitivity is usually used in C++.

I'm not sure what you mean by transitivity. Do you mean that since C
inherits from B inherits from A, then can you use C as a type of A?

Yes, that's what polymorphism is - public inheritance allows that. It
represents an "is a" relationship.

C is a B, B is an A, therefore C is an A.

An "Audi A4" is a Car. A Car is a Vehicle. An "Audi A4" is a Vehicle.


Please try to make more effort with your posting. At least attempt to
compile what you have written, and then copy-paste it.

Ben Pope
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top