473,406 Members | 2,843 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,406 software developers and data experts.

creating a pointer to a base class

Hi, I'm trying to do something like this:

class A {
public:
virtual void f() {
// do something
}
};
class B : public A {
// some methods here
};

class C {
public:
C(A* a)
{
a_ = new WHAT?!?! // could be either A or B!!
}
A* a_;
};

In general, there could be more classes derived from A or B.... Assuming
there are copy constructors and assignment operator defined for A and B.
How can I initalize a_? do I have to resort to RTTI if-else ladders!?
There is got to be a better way!

thanks

Smith
Jul 19 '05 #1
3 2118

Użytkownik "john smith" <as**@asdf123asdf.net> napisał w wiadomości
news:bg***********@news.eecs.umich.edu...
Hi, I'm trying to do something like this:

class A {
public:
virtual void f() {
// do something
}
};
class B : public A {
// some methods here
};

class C {
public:
C(A* a)
{
a_ = new WHAT?!?! // could be either A or B!!
}
A* a_;
};

In general, there could be more classes derived from A or B.... Assuming
there are copy constructors and assignment operator defined for A and B.
How can I initalize a_? do I have to resort to RTTI if-else ladders!?
There is got to be a better way!


Maybe You should write the "clone function for every class (A and B). In
class A
A* A::clone()
{
A* aa = new A();
//copy members variables to aa;
return aa;
}

In class B
B* B::clone()
{
B* bb = new B();
//copy members variables to bb;
return bb;
}

And inyour class C
C::C(A* a)
{
a_ = a.clone();
}
Jul 19 '05 #2


eftewuer wrote:


Maybe You should write the "clone function for every class (A and B). In
class A
A* A::clone()
{
A* aa = new A();
//copy members variables to aa;
return aa;
}


A* A::clone()
{
return new A( *this );
}

is simpler and less error prone.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3

Użytkownik "john smith" <as**@asdf123asdf.net> napisał w wiadomości
news:bg***********@news.eecs.umich.edu...
one question... does close need to be virtual? Seems like that's
necessary...

thanks.

smith.
Yes.
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


eftewuer wrote:


Maybe You should write the "clone function for every class (A and B). In class A
A* A::clone()
{
A* aa = new A();
//copy members variables to aa;
return aa;
}


A* A::clone()
{
return new A( *this );
}

is simpler and less error prone.
--
Karl Heinz Buchegger
kb******@gascad.at


Jul 19 '05 #4

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

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
5
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
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...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.