473,503 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do not want to use pointers

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
7 1479

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3082
by: LinusLee | last post by:
class CAnalyzer { // attribute private: public: // behavior private: void NullFunc(NODE *pNode); void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void (*postFunc)(NODE *node));
3
3431
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
5042
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
4059
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
2807
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
5000
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
3494
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
12986
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
21
1969
by: Lee.kain | last post by:
I want to learn C++! does anyone have any advice? Lee
0
7072
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
7319
jinu1996
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...
1
6979
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
5570
agi2029
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,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
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...

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.