help upcasting

M

Mark

I can't seem to get this subclass to upcast to a baseclass... see
comments below

#include <cstdlib>
#include <iostream>

#include "Base.h"
#include "Sub1.h"

using namespace std;

void f(Base* b)
{
cout << "ok";
}

int main(int argc, char *argv[])
{
Sub1* s1;
Base* b;
f(s1); // how come I can do this
s1 = b; // but not this??
system("PAUSE");
return EXIT_SUCCESS;
}

the compiler complains..
19 invalid conversion from `Base*' to `Sub1*'

and if you need the header files..

#ifndef SUB1_H
#define SUB1_H

#include "Base.h"

class Sub1 : public Base
{
public:
Sub1* clone();
};

#endif

#ifndef BASE_H
#define BASE_H

class Base
{
public:
Base* clone();
};

#endif

help appreciated.
 
A

Andre Kostur

I can't seem to get this subclass to upcast to a baseclass... see
comments below

#include <cstdlib>
#include <iostream>

#include "Base.h"
#include "Sub1.h"

using namespace std;

void f(Base* b)
{
cout << "ok";
}

int main(int argc, char *argv[])
{
Sub1* s1;
Base* b;
f(s1); // how come I can do this

Because you're upcasting from a derived class to a base.
s1 = b; // but not this??

That's a downcast, not an upcast. You are casting (or attempting to)
from a base to a derived class. Can't be done implicitly.
system("PAUSE");
return EXIT_SUCCESS;
}

the compiler complains..
19 invalid conversion from `Base*' to `Sub1*'

Uh... the 19 would be in the rest of your code....
 
M

Mark

Andre said:
I can't seem to get this subclass to upcast to a baseclass... see
comments below

#include <cstdlib>
#include <iostream>

#include "Base.h"
#include "Sub1.h"

using namespace std;

void f(Base* b)
{
cout << "ok";
}

int main(int argc, char *argv[])
{
Sub1* s1;
Base* b;
f(s1); // how come I can do this

Because you're upcasting from a derived class to a base.
s1 = b; // but not this??

That's a downcast, not an upcast. You are casting (or attempting to)
from a base to a derived class. Can't be done implicitly.
system("PAUSE");
return EXIT_SUCCESS;
}

the compiler complains..
19 invalid conversion from `Base*' to `Sub1*'

Uh... the 19 would be in the rest of your code....
and if you need the header files..

#ifndef SUB1_H
#define SUB1_H

#include "Base.h"

class Sub1 : public Base
{
public:
Sub1* clone();
};

#endif

#ifndef BASE_H
#define BASE_H

class Base
{
public:
Base* clone();
};

#endif

help appreciated.

wait..what?
how is s1 = b; a downcast? i'm trying to make my subclass point to the
baseclass.. that moves it UP the hiearchy, no??
you're suggesting that b = s1; is an upcast?
could you perhaps elaborate a bit?

"19" is the line number.
 
P

Pete Becker

Mark said:
wait..what?
how is s1 = b; a downcast? i'm trying to make my subclass point to the
baseclass.. that moves it UP the hiearchy, no??

It depends on which way you draw your class hierarchies, which is why
it's such a confusing term.
you're suggesting that b = s1; is an upcast?
could you perhaps elaborate a bit?

b = s1 is a valid assignment. It converts a pointer to a derived type
into a pointer to base. s1 = b is not valid. It attempts to convert a
pointer to a base type into a pointer to derived type, which the
language does not allow without a cast. s1 = (Sub1*)b is valid, because
it has an explicit cast. It may well be an error, though, if b does not,
in fact, point to an object of type Sub1. That's the risk you take when
you use that cast.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
B

BobR

Mark wrote in message
Andre said:
I can't seem to get this subclass to upcast to a baseclass... see
comments below

#include <cstdlib>
#include <iostream>
#include "Base.h"
#include "Sub1.h"
using namespace std;

void f(Base* b){
cout << "ok";
}

int main(int argc, char *argv[]){
Sub1* s1;
Base* b;
f(s1); // how come I can do this

Because you're upcasting from a derived class to a base.
s1 = b; // but not this??

That's a downcast, not an upcast. You are casting (or attempting to)
from a base to a derived class. Can't be done implicitly.
system("PAUSE");
return EXIT_SUCCESS;
}

the compiler complains..
19 invalid conversion from `Base*' to `Sub1*'
and if you need the header files..

#ifndef SUB1_H
#define SUB1_H
#include "Base.h"
class Sub1 : public Base { public:
Sub1* clone();
};
#endif

#ifndef BASE_H
#define BASE_H
class Base{ public:
Base* clone();
};
#endif

help appreciated.

wait..what?
how is s1 = b; a downcast? i'm trying to make my subclass point to the
baseclass.. that moves it UP the hiearchy, no??
you're suggesting that b = s1; is an upcast?
could you perhaps elaborate a bit?


class shape {..... virtual ~shape(){} };
class circle : shape {.....};

Usually 'diagramed' like this:

|-------------|
| shape |
|-------------|
^
|
|-------------|
| circle |
|-------------|

Going from 'circle' to 'shape' is upcasting.
Going from 'shape' to 'circle' is downcasting.

A 'circle' knows it is a 'shape' [1], but a 'shape':

"Hey man, I must have been drunk, 'cause I don't know how many kids I got out
there, or what they look like! Can you give me a clue (down cast)[2]".

shape *sh;
circle *cr;
circle C1;
sh = &C1; // upcast
cr = dynamic_cast<circle*>( sh ); // downcast
if( cr ){
std::cout<<"OK"<<std::endl;
}
else{
std::cout<<"That wern't no circle, dude!!"<<std::endl;
}
 
M

Mark

Pete said:
It depends on which way you draw your class hierarchies, which is why
it's such a confusing term.


b = s1 is a valid assignment. It converts a pointer to a derived type
into a pointer to base. s1 = b is not valid. It attempts to convert a
pointer to a base type into a pointer to derived type, which the
language does not allow without a cast. s1 = (Sub1*)b is valid, because
it has an explicit cast. It may well be an error, though, if b does not,
in fact, point to an object of type Sub1. That's the risk you take when
you use that cast.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.

heh. i think it's easier to think about it like

a = b
as
a <- b

it's "b" that needs to be cast (if they are not the same type), not
"a".

i think i had the mental picture

a -> b

where a is converted to type b, and then they a gets the value of
b...but that's clearly wrong :)

thanks guys.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top