By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,106 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,106 IT Pros & Developers. It's quick & easy.

Do not want to use pointers

P: n/a
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
Dec 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

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;

Dec 13 '06 #2

P: n/a
Erdal Mutlu wrote:
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.

--
Ian Collins.
Dec 13 '06 #3

P: n/a
Ian Collins wrote:
Erdal Mutlu wrote:
>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.
OK
>
>>
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.
When I sent this message I have realized that I could use smart pointers.
>
>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.

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
Dec 13 '06 #4

P: n/a

Erdal Mutlu napsal:
Ian Collins wrote:
Erdal Mutlu wrote:
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.

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

When I sent this message I have realized that I could use smart pointers.
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.


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.

Dec 13 '06 #5

P: n/a
Ondra Holub wrote:
>
Erdal Mutlu napsal:
>Ian Collins wrote:
Erdal Mutlu wrote:
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.

OK
>

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.

When I sent this message I have realized that I could use smart pointers.
>
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.


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.
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
Dec 13 '06 #6

P: n/a

Erdal Mutlu napsal:
Ondra Holub wrote:

Erdal Mutlu napsal:
Ian Collins wrote:

Erdal Mutlu wrote:
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.

OK



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.

When I sent this message I have realized that I could use smart pointers.


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.

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.

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);
}

Dec 13 '06 #7

P: n/a
Ondra Holub wrote:
>
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
Dec 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.