Assigining heap objects to Abstract parent with tenary op

Get answers to all your basic programming questions. No Ogre questions, please!
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

Assigining heap objects to Abstract parent with tenary op

Post by PhilipLB »

Hi,

assuming AbstractFoo is the parent class of FooA and FooB, why does this refuse to compile?

The error is:
1>Bla.cpp(149): error C2446: ':' : no conversion from 'FooB *' to 'FooA *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Why does it want to cast like this?!

Wheras this version works fine:

Code: Select all

Foo *foo = bar ? reinterpret_cast<Foo*>(new FooA()) : new FooB();
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.
User avatar
saejox
Goblin
Posts: 260
Joined: Tue Oct 25, 2011 1:07 am
x 36

Re: Assigining heap objects to Abstract parent with tenary o

Post by saejox »

edit: i read your question wrong sorry :(

Tenary operator cannot decide resulting type between two expressions.
It looks at two expressions, they both have different types, and no common base.
There is a base shared but compiler does not check that unless explicitly specified.
Last edited by saejox on Mon Jul 22, 2013 11:05 am, edited 1 time in total.
Nimet - Advanced Ogre3D Mesh/dotScene Viewer
asPEEK - Remote Angelscript debugger with html interface
ogreHTML - HTML5 user interfaces in Ogre
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

Re: Assigining heap objects to Abstract parent with tenary o

Post by PhilipLB »

Hm, maybe I was expecting this to work like in Java. At least the compiler from Visual Studio 2010 doesn't complain?
Would be a bit of a surprise if

Code: Select all

Foo *foo = new FooA();
didn't work...
Can anyone confirm that this shouldn't work?

To the tenary operator: Note that the compiler complains that he can not cast FooB* to FooA* which is reasonable. But why does he want to cast like this anyway?
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.
User avatar
saejox
Goblin
Posts: 260
Joined: Tue Oct 25, 2011 1:07 am
x 36

Re: Assigining heap objects to Abstract parent with tenary o

Post by saejox »

PhilipLB wrote:Hm, maybe I was expecting this to work like in Java. At least the compiler from Visual Studio 2010 doesn't complain?
Would be a bit of a surprise if

Code: Select all

Foo *foo = new FooA();
didn't work...
Can anyone confirm that this shouldn't work?

To the tenary operator: Note that the compiler complains that he can not cast FooB* to FooA* which is reasonable. But why does he want to cast like this anyway?
Btw, i editted my post.

You want compiler to find common base of FooA and FooB. It just won't, that a expensive job, and its ambiguous.
Look at this for instance

Code: Select all

class Dad { }
class Foo : public Dad { }
class Bar : public Foo {}
class FooA : public Foo {}
class FooB : public Bar {}

Foo * foo = r ? new FooA() : new FooB();
Compiler doesn't want to walk back. It never does, it's simple like that.
Even if it did there are two common bases here.
Nimet - Advanced Ogre3D Mesh/dotScene Viewer
asPEEK - Remote Angelscript debugger with html interface
ogreHTML - HTML5 user interfaces in Ogre
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

Re: Assigining heap objects to Abstract parent with tenary o

Post by PhilipLB »

But it's an easy check if this is assignable or not? Just look if Foo* is a parent class and that's it...
I don't get why the tenary operator has to check here something at all? In Java, this works just fine...
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.
User avatar
saejox
Goblin
Posts: 260
Joined: Tue Oct 25, 2011 1:07 am
x 36

Re: Assigining heap objects to Abstract parent with tenary o

Post by saejox »

PhilipLB wrote:But it's an easy check if this is assignable or not? Just look if Foo* is a parent class and that's it...
I don't get why the tenary operator has to check here something at all? In Java, this works just fine...
But lets say it found a class named Bar as a common base. But you are trying to assign to Foo.
Is it going to an implicit cast or look for another common base and give error is can't.
That would mean to look at the lefthand again, that just won't do c++ is stubborn. It doesn't walk back.

These stupid errors all come from C++ being a single pass compiler.
It needs to know every thing a single pass, it doesn't like walking back. Doesn't even looks back at the left-hand of the assignment.
Terrible trait of the c++ compiler we all need to live with.
Nimet - Advanced Ogre3D Mesh/dotScene Viewer
asPEEK - Remote Angelscript debugger with html interface
ogreHTML - HTML5 user interfaces in Ogre
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

Re: Assigining heap objects to Abstract parent with tenary o

Post by PhilipLB »

Hm, ok, at least a reason. :)
Stupid ancient language... ;)
Thanks!
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.