473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

help upcasting

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.

Oct 31 '06 #1
5 2487
"Mark" <mn*******@gmail.comwrote in news:1162340151.361660.142970
@f16g2000cwb.googlegroups.com:
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.
Nov 1 '06 #2

Andre Kostur wrote:
"Mark" <mn*******@gmail.comwrote in news:1162340151.361660.142970
@f16g2000cwb.googlegroups.com:
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.

Nov 1 '06 #3
Mark wrote:
>
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.
Nov 1 '06 #4

Mark wrote in message
<11**********************@i42g2000cwa.googlegroups .com>...
>
Andre Kostur wrote:
>"Mark" <mn*******@gmail.comwrote in news:1162340151.361660.142970
@f16g2000cwb.googlegroups.com:
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;
}

--
Bob R
POVrookie
Nov 1 '06 #5

Pete Becker wrote:
Mark wrote:

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.
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.

Nov 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
Lets suppose I have an class called DatastoreSelect. It has a method called getNextRow(). This method is not abstract, it is fully implemented. It also has a method called setInfoToBeSought, which...
21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
2
by: Judith | last post by:
Hi there everyone I've got: class A { ... }; class B : public A { ... }
4
by: juli jul | last post by:
Hello, I did the following with Class A which is a parent and B inherits from it: B b1=new B(); A a1=new A(); a1=b1; Is it an upcasting in c#? Thank you!
4
by: Amod | last post by:
I want to call a child class constructor using the base class instance ... whats the xact mechanism to perform this function ? Regards, Amod
3
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
4
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
0
by: Juha Nieminen | last post by:
If I'm not mistaken, general OOP wisdom says that upcasting should usually be avoided if possible. I have a situation, however, where I can't think of a better way than upcasting for this specific...
1
by: alireza6485 | last post by:
Hi, can u help me with polymorphism and up-casting please. Bipeds scream birds fly bird bd =new bied; Bipeds bp bp=bd(upcasting)//what does this line do?is it saying that the biped we have is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.