473,385 Members | 1,907 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,385 software developers and data experts.

putting a subclass in a pointer to baseclass?

This is a pretty dumb question to ask, but I'll do it anyways.

I've got a few superclasses and a subclass. Let's say...

class BaseFruits {}
class Apple : public BaseFruits {}
class Orange : public BaseFruits {}


Now in my code I've got BaseFruits *currentFruit;

I THOUGHT that would mean I could stick Apple into currentFruit, but it won't let me, it throws an error saying it can't convert. The problem is I don't want to use dynamic_casts or anything like that because it will never know if it's an Apple, Orange, Grapefruit, whatever... So what do I do? For some reason, I thought since Apple was a subclass of BaseFruits, I could put it in there, but it's been a while since I've messed with this stuff and I figured after 5 frustrating hours Friday and a few more today I should just suck it up and ask for help.

Thanks!!
Apr 6 '08 #1
1 2066
weaknessforcats
9,208 Expert Mod 8TB
Works for me:
Expand|Select|Wrap|Line Numbers
  1. class BaseFruits {}; 
  2. class Apple : public BaseFruits {};
  3. class Orange : public BaseFruits {};
  4.  
  5.  
  6. int main()
  7. {
  8.     Apple* a = new Apple;
  9.     BaseFruits* currentFruit = a;
  10. }
  11.  
What are you doing different?
Apr 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
4
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. ...
5
by: mandatory | last post by:
hi, i have a pointer to a class, which is dynamically allocated with new. myClass *mC = new myClass(); What if i at a later point in my code i have this: void *pointer;
5
by: g18c | last post by:
I am trying to have a pointer to a member variable, however i will be deriving a number of classes from a base class. Whilst the code below works, i am wondering if there is a better way of doing...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
1
by: Paul_P | last post by:
Hello all, We are using Managed and Unmanaged C++ code, on Windows XP SP2, VC++ .NET 2003. In the SubClass we are trying to override a virtual method of the BaseClass that has an unmanaged type...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.