I am trying to put together a system that involves 3 classes derived
form a common base class, which is working fine.
Except that I want to store them in a map and I find that I cannot do
this as I thought I could.
I have the following classes
class base_class{}
class a : base_class{int aa}
class b : base_class{char bb}
class c : base_class{doub le cc}
Then I create a map like this
typedef std::map<std::s tring,base_clas s, std::less<std:: string
map_objects;
typedef map_of_objects: :value_type map_of_objects_ value;
I insert a object value to the map like this
a object = a();
objects.insert( map_of_objects_ value("name", object));
Then I read it back like this
a object;
map_of_objects: :iterator itor = objects.find("n ame");
if (itor != objects.end())
{
object = (*itor).second;
}
But when I check the class values they are all wrong. The only way i
can get this to work is to change the map typedef to use class a but
then class b and class c do not work.
In Java because everything has a common base object, these sort of
problems do not come about but I thought I could do a similar thing by
creating a base class.
How can I do what I want to do or will I have to place pointers to
objects in the map instead of the actual objects?
TIA,
Tom 4 3903
If you want a container of base classes that stores derived ones, you
must make a container of base class pointers and store pointers to
derived objects created with "new". Then, you must make sure that
removing from your container calls delete on the item before. The
dectructors _must_ be virtual in this case!
Gernot Frisch wrote:
If you want a container of base classes that stores derived ones, you
must make a container of base class pointers and store pointers to
derived objects created with "new". Then, you must make sure that
removing from your container calls delete on the item before. The
dectructors _must_ be virtual in this case!
Thanks, I thought that was going to be the case but I guess I have got
used to using Java now and one of the few things I prefer in Java over
C++ is the use of the base object :)
Then again I have never really liked dealing with pointers but that is
a personal thing based mostly one little things like it's easier to
type a "." than a "->" operator ;)
Tom wrote:
>
Gernot Frisch wrote:
>If you want a container of base classes that stores derived ones, you must make a container of base class pointers and store pointers to derived objects created with "new". Then, you must make sure that removing from your container calls delete on the item before. The dectructors _must_ be virtual in this case!
Thanks, I thought that was going to be the case but I guess I have got
used to using Java now and one of the few things I prefer in Java over
C++ is the use of the base object :)
Then again I have never really liked dealing with pointers but that is
a personal thing based mostly one little things like it's easier to
type a "." than a "->" operator ;)
Part of the trouble is that we cannot overload the dot operator. Otherwise,
we could write smart references and classes whose objects behave like
polymorphic values (not references). This way, one could take care of many
problems that currently arise with the use of polymorphic types in standard
containers.
Best
Kai-Uwe Bux
Gernot Frisch wrote:
If you want a container of base classes that stores derived ones, you
must make a container of base class pointers and store pointers to
derived objects created with "new". Then, you must make sure that
removing from your container calls delete on the item before. The
dectructors _must_ be virtual in this case!
Easiest way is to have a collection of shared_ptr< base or map< key,
shared_ptr< base . shared_ptr is in tr1 and boost. Not yet in std
but will be one day.
Note you cannot use auto_ptr.
boost does also provide some pointer-containers for you. Look up
ptr_containers at www.boost.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brad Kartchner |
last post by:
I'm attempting to write a program with several child classes derived from a
single parent class. The parent class has a couple of variables that need
to be present in each of the child classes. However, I need the objects
created from each of the derived classes to share these variables with other
objects of the same class but not with other...
|
by: Nathan Bullock |
last post by:
Hi,
I have a base class, say Base and there are two classes, say Class1
and Class2 which are derived from Base. Is there any way for me, say
from a static method in Base, to get a list of all classes derived
from Base?
class abstract Base
{
static public ArrayList AllDerived()
|
by: William Stacey |
last post by:
I know the following is not allowed, but shouldn't it be? sharedObject is
part of Derived and should be able to be set in the constructor - no? tia
public abstract class Base1
{
protected readonly object sharedObject;
}
public class Derived : Base1
{
|
by: z. f. |
last post by:
Hi,
i have a class that is derived from System.Web.UI.Page, and this is the
class i use in my application as PageBase.
all other page classes are deriverd from my PageBase instead of the original
System.Web.UI.Page in order to have common checks in the page base.
i make securirty checks in the page base page_load event.
if the security...
|
by: Larry Woods |
last post by:
I have a method in my base class that I want ALL derived classes to use.
But, I find that I can create a "Shadow" method in my derived class that
"overrides" the method in my base class. Can't figure out what attribute to
put on the base class method to prevent this.
TIA,
Larry Woods
| |
by: Manuel |
last post by:
Hi!
If I've a vector filled with abstract classes, can I push in it the
derived classes too? Even if derived classes have new methods?
I've done some experiments, and it seem I can push the derived classes,
but I can use them only calling the methods declared in abstract class
(the new methods declared only in derived classes return an...
|
by: ivan.leben |
last post by:
I want to write a Mesh class using half-edges. This class uses three
other classes: Vertex, HalfEdge and Face. These classes should be
linked properly in the process of building up the mesh by calling Mesh
class functions. Let's say they point to each other like this:
class Vertex {
HalfEdge *edge;
};
class HalfEdge {
Vertex* vert;
|
by: desktop |
last post by:
On this page:
http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html
Shape specify the virtual function:
virtual double Intersect( const Shape& s) = 0;
then the derived class Circle also specify:
|
by: Bob Johnson |
last post by:
I have a base class that must have a member variable populated by, and only
by, derived classes.
It appears that if I declare the variable as "internal protected" then the
base class *can* populate the variable, but the population is not *required*
by the derived class (which must be the case).
What would meet the requirements is if I...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |