compilation error

G

Gary Wessle

Hi

I am trying to return a derived type pointer as the base type pointer
and the compiler is complaining, here is what I mean.

class A {
public:
A(){}
virtual void lol()=0;
};

class B : public A {
public:
B(): A() {}
void lol();
};

class C {
public:
C(){}
A* est(){
B b;
b.lol();
return &b;
}
};

filename.cpp:34: error: invalid conversion from ‘A*’ to ‘B*’

B and A habe "is-a" relationship, that is to say B is-a A, thus why it
is not working?

thanks
 
K

Kai-Uwe Bux

Gary said:
Hi

I am trying to return a derived type pointer as the base type pointer
and the compiler is complaining, here is what I mean.

class A {
public:
A(){}
virtual void lol()=0;
};

class B : public A {
public:
B(): A() {}
void lol();
};

class C {
public:
C(){}
A* est(){
B b;
b.lol();
return &b;

This is a BadIdea(tm) regardless of compilation errors: the object b goes
out of scope and the pointer you return points into nirvana.
}
};

filename.cpp:34: error: invalid conversion from ?A*? to ?B*?

Note: this snippet does not have 34 lines. You are not posting the actual
code. In fact, there is no conversion from A* to B* in the code posted.
Your compilation error is somewhere else.
B and A habe "is-a" relationship, that is to say B is-a A, thus why it
is not working?

Because direction matters: A might not be B. Presumably, on line 34 you do
something like

B* b_ptr = c.est();

and that would trigger the error.


Best

Kai-Uwe Bux
 
G

Gavin Deane

Gary said:
Hi

I am trying to return a derived type pointer as the base type pointer
and the compiler is complaining, here is what I mean.

class A {
public:
A(){}
virtual void lol()=0;
};

class B : public A {
public:
B(): A() {}
void lol();
};

class C {
public:
C(){}
A* est(){
B b;
b.lol();
return &b;
}
};

filename.cpp:34: error: invalid conversion from 'A*' to 'B*'

B and A habe "is-a" relationship, that is to say B is-a A, thus why it
is not working?

What compiler are you using? Comeau online and MSVC++8 compile this
with no errors [*] for me. Are you sure the error message you posted
pertains to this code? The error message complains about line 34. I
only see 24 lines of code, but you could have copied this from within a
larger source. More than that, the return statement in C::est coverts a
B* to an A* but your error message complains about attempting to
convert the other way, from A* to B*.

[*] There is still a serious problem with your code. While I can
compile it with no errors, Comeau online very sensibly issues a warning
about the return &b; statement:
"warning: returning pointer to local variable"

When the est function returns, the object b local to that function is
destroyed. You return a pointer to the object but after the function
ends, the object no longer exists. Any attempt to get at the object by
dereferencing the pointer has undefined behaviour.

Gavin Deane
 
D

David Harmon

On 27 Nov 2006 05:49:50 +1100 in comp.lang.c++, Gary Wessle
I am trying to return a derived type pointer as the base type pointer
and the compiler is complaining, here is what I mean.

No, there are fewer than 34 lines in the sample you posted; your error
is apparently elsewhere.
A* est(){
B b;
b.lol();
return &b;
}

Bad news, you are returning a pointer to a local variable that is
destroyed immediately before the return. Any attempt to use the return
value is undefined behavior.
filename.cpp:34: error: invalid conversion from ‘A*’ to ‘B*’

B and A habe "is-a" relationship, that is to say B is-a A, thus why it
is not working?

One way only. Any B* can be converted to an A*, but not the other way
around, because the A* may point to an instance of A and not a B, or to
an instance of some other derived class and not a B.
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top