Pointers and variables

  • Thread starter Dmitri Islentyev
  • Start date
D

Dmitri Islentyev

Hello everyone!

Please consider the following code:

class Base {
public:
Base(int x);
virtual int foo() { return 17; }
};

class Derived: public Base {
Derived(int x);
int foo() { return 23; }
};

class Maker {
static Base CreateBase(bool whether)
{
if(whether)
return Base(0);
else
return Derived(1);
}
};

// client

Base b=Maker.CreateBase(1);
b.foo(); // returns 17
Base d=Maker.CreateBase(0);
d.foo(); // returns 17

There`s no vtable in value variables, the solution is to use pointers.
But what`s the method to delete returned pointers, if, for example,
I return a cursor class from operator [] just for one function call
from returned cursor instance?
 
R

Rolf Magnus

Dmitri said:
Hello everyone!

Please consider the following code:

class Base {
public:
Base(int x);
virtual int foo() { return 17; }
};

class Derived: public Base {
Derived(int x);
int foo() { return 23; }
};

class Maker {
static Base CreateBase(bool whether)
{
if(whether)
return Base(0);
else
return Derived(1);
}
};

This function creates either a Base or a Derived, and then will make a
copy of the Base part of it and return that. This is called slicing.
Since the return type is Base, it's _always_ a Base that is returned.
// client

Base b=Maker.CreateBase(1);
b.foo(); // returns 17
Base d=Maker.CreateBase(0);
d.foo(); // returns 17

There`s no vtable in value variables, the solution is to use pointers.

No, that doesn't have to do with the vtable. The problem is the slicing
that I mentioned above.

Btw: the values of bool are true and false. 1 and 0 will be converted to
true and false, but it's more clear to write true and false directly.
But what`s the method to delete returned pointers, if, for example,
I return a cursor class from operator [] just for one function call
from returned cursor instance?

I'm not sure what exactly you mean. In your above example, you could
allocate your object with new and return a pointer to it. Then the
caller will be responsible for deleting the object. Another solution is
a smart pointer like boost::shared_ptr, which is a reference counted
smart pointer class. It will automatically delete the object if no
shared_ptr points to it anymore.
 
J

John Harrison

Dmitri Islentyev said:
Hello everyone!

Please consider the following code:

class Base {
public:
Base(int x);
virtual int foo() { return 17; }
};

class Derived: public Base {
Derived(int x);
int foo() { return 23; }
};

class Maker {
static Base CreateBase(bool whether)
{
if(whether)
return Base(0);
else
return Derived(1);
}
};

Doesn't work, Derived is converted to a Base on exit from CreateBase
// client

Base b=Maker.CreateBase(1);
b.foo(); // returns 17
Base d=Maker.CreateBase(0);
d.foo(); // returns 17

There`s no vtable in value variables, the solution is to use pointers.
But what`s the method to delete returned pointers, if, for example,
I return a cursor class from operator [] just for one function call
from returned cursor instance?

Use smart pointers. Smart pointers delete themselves.

Get a good C++ book and read about smart pointers (aka reference counting).
For instance More Effective C++ by Scott Meyers.

You could also look at the smart pointer library available from boost,
www.boost.org

john
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top