Do not want to use pointers

E

Erdal Mutlu

Hi,

I am trying to design a base class (interface) with two or more subclasses
as follows:

class A {
....
virtual static A* getByName(const string x)=0 const;
}

class B : public A {
....
virtual static A* getByName(string x) const;
}

class C : public A {
....
virtual static A* getByName(string x) const;
}

....
void doSomething(const A* a);


The getByName() member function is a utility function, which creates a
pointer to an object of type A. The B and C classes are going to be used
at the beggining, when they are created, afterwards I would like to use the
base class (A) as arguments to functions. Is there a way of doing this, but
without returning a pointer to an A object? The problem is that care must be
taken for deleting pointers at some point. I would like to use
constructor/destructor do the job of creating and auto deleting of objects.
If I return an A object, then the B or C part of the object is going to be
sliced off.

Best regards.
Erdal Mutlu
 
O

Ondra Holub

Erdal Mutlu napsal:
Hi,

I am trying to design a base class (interface) with two or more subclasses
as follows:

class A {
...
virtual static A* getByName(const string x)=0 const;
}

class B : public A {
...
virtual static A* getByName(string x) const;
}

class C : public A {
...
virtual static A* getByName(string x) const;
}

...
void doSomething(const A* a);


The getByName() member function is a utility function, which creates a
pointer to an object of type A. The B and C classes are going to be used
at the beggining, when they are created, afterwards I would like to use the
base class (A) as arguments to functions. Is there a way of doing this, but
without returning a pointer to an A object? The problem is that care must be
taken for deleting pointers at some point. I would like to use
constructor/destructor do the job of creating and auto deleting of objects.
If I return an A object, then the B or C part of the object is going to be
sliced off.

Best regards.
Erdal Mutlu

If you create virtual destructor in base class, everything will be
deleted correctly. You can try it this way:

class A {
....
virtual static A* getByName(const string x)=0 const;
virtual ~A() { std::cout << "A::~A()\n"; }

}

class B : public A {
....
virtual static A* getByName(string x) const;
virtual ~B() { std::cout << "B::~B()\n"; }

}

class C : public A {
....
virtual static A* getByName(string x) const;
virtual ~C() { std::cout << "C::~C()\n"; }

}

A* pa = new B(some arguments);
delete pa;
 
I

Ian Collins

Erdal said:
Hi,

I am trying to design a base class (interface) with two or more subclasses
as follows:

class A {
....
virtual static A* getByName(const string x)=0 const;

You can't have virtual static. One or the other, but not both.
The getByName() member function is a utility function, which creates a
pointer to an object of type A.

Then a better name would be createByName, get implies the object already
exists.

The B and C classes are going to be used
at the beggining, when they are created, afterwards I would like to use the
base class (A) as arguments to functions. Is there a way of doing this, but
without returning a pointer to an A object? The problem is that care must be
taken for deleting pointers at some point.

Then use a smart pointer type.
I would like to use
constructor/destructor do the job of creating and auto deleting of objects.
If I return an A object, then the B or C part of the object is going to be
sliced off.
Sound like you want some form of object factory class.
 
E

Erdal Mutlu

Ian said:
You can't have virtual static. One or the other, but not both.
OK


Then a better name would be createByName, get implies the object already
exists.

The B and C classes are going to be used

Then use a smart pointer type.

When I sent this message I have realized that I could use smart pointers.
Sound like you want some form of object factory class.


Yes, the problem falls in this class, but I still could not find an elagant
way of solving it, I believe there is such way. The idea of using smart
pointers is good, but not all programmers use it, so the problem still
exists for those who do not use them. And I still have to pass pointers to
functions, instead of (const) references, which I would prefer to.

Best regards.
Erdal Mutlu
 
O

Ondra Holub

Erdal Mutlu napsal:
When I sent this message I have realized that I could use smart pointers.



Yes, the problem falls in this class, but I still could not find an elagant
way of solving it, I believe there is such way. The idea of using smart
pointers is good, but not all programmers use it, so the problem still
exists for those who do not use them. And I still have to pass pointers to
functions, instead of (const) references, which I would prefer to.

Well, when some solution for any problem exists and someone does not
use this solution, he can either start to use it or try to reinvent
wheel and find other solution.

With smart pointers you do not pass pointers to functions (I think you
mean passing function parameters). You pass references to instances of
smart pointers.
 
E

Erdal Mutlu

Ondra said:
Erdal Mutlu napsal:

Well, when some solution for any problem exists and someone does not
use this solution, he can either start to use it or try to reinvent
wheel and find other solution.

With smart pointers you do not pass pointers to functions (I think you
mean passing function parameters). You pass references to instances of
smart pointers.

OK, but does this means that the smart pointer solution is the only
alternative? Is there no other way to use polimorphisim and avoid depending
on pointer and/or smart pointer usage?

Best regards.
Erdal Mutlu
 
O

Ondra Holub

Erdal Mutlu napsal:
OK, but does this means that the smart pointer solution is the only
alternative? Is there no other way to use polimorphisim and avoid depending
on pointer and/or smart pointer usage?

Best regards.
Erdal Mutlu

You can use references. References are good, but have some problem
here. Referenced instance must exist somewhere (and the question is
where to define and store it). Reference is perfect as function
parameter, because it is effective (internaly it is "something" like
pointer, but more safe). If you need only something like this, use
them:

class A
{
// Something
};

class B : public A
{
};

class C: public A
{
};

void SomeFunction(A& a)
{
a.method1();
a.method2();
}

void MyFunction()
{
B b;
C c;

SomeFunction(a);
SomeFunction(b);
}

You can even use it this way:

void MyFunction2()
{
class D: public A
{
// Some methods are here (especialy virtual methods)
};

D d;
SomeFunction(d);
}
 
E

Erdal Mutlu

Ondra said:
You can use references. References are good, but have some problem
here. Referenced instance must exist somewhere (and the question is
where to define and store it). Reference is perfect as function
parameter, because it is effective (internaly it is "something" like
pointer, but more safe). If you need only something like this, use
them:

class A
{
// Something
};

class B : public A
{
};

class C: public A
{
};

void SomeFunction(A& a)
{
a.method1();
a.method2();
}

void MyFunction()
{
B b;
C c;

SomeFunction(a);
SomeFunction(b);
}

You can even use it this way:

void MyFunction2()
{
class D: public A
{
// Some methods are here (especialy virtual methods)
};

D d;
SomeFunction(d);
}


I think I need a factory function (createByName()) or class which is going
to create B or C. Actually I am trying to represent the Internet address
(A=InetAddress, B=Inet4Address, C=Inet6Address). So when I get a string,
say a hostname, the factory function or class should take this string and
return an InetAddress object. I know how to to this, when the factory
function or class returns a pointer to A (InetAddress) and this pointer can
be used as parameters to functions etc. At the end be deleted. I was
wondering whetere I could use another aproach (not smart pointers)
to solve this kind of problems. I face this same kind of problem while
designing database utility classes. I used the pointer approach for their
implementation. At that time I didn't know about smart pointers and
had a lot of delete (clean up) commands spread accross application code,
which used those classes. I feel that I am one small step behind a better
design.

Best regards.
Erdal Mutlu
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top