473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Pointers and variables

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?
Jul 22 '05 #1
2 1266
Dmitri Islentyev wrote:
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.

Jul 22 '05 #2

"Dmitri Islentyev" <di*****@sampo.karelia.ru> wrote in message
news:13**************************@posting.google.c om...
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
11
by: Satish Kumar | last post by:
Can any one give me indetail about, what are pointers? How are pointers useful in C prgramming? How do they work? i had gone through one book, i am a bit confused. Regards, Satish...
6
by: Mark Broadbent | last post by:
hi all. Ive been looking into the use of pointers to try and get my head around them which has led me onto a question. 1. How do you know what memory position C# compiler will use for each...
8
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.