473,385 Members | 1,867 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,385 software developers and data experts.

dereferencing pointer and object creation

When I dereference a pointer, does it make a copy of the object?

Say I had a singleton, and wanted an static method to retrieve it from
the class.

class foo {
private:
static foo *bar;

foo() {} // no public creation!
public:
~foo() { delete bar; }
foo &instance() { return *bar; }
};

foo foo::bar = new foo;

Will instance create a copy of the object when it's dereferenced?

Thanks,

--John Ratliff
Aug 10 '05 #1
3 3388
"John Ratliff" <us**@example.net> schrieb im Newsbeitrag
news:j8kKe.236394$x96.154050@attbi_s72...
When I dereference a pointer, does it make a copy of the object?

Say I had a singleton, and wanted an static method to retrieve it from
the class.

class foo {
private:
static foo *bar;

foo() {} // no public creation!
public:
~foo() { delete bar; }
foo &instance() { return *bar; }
IMHO instance should be a static function.
};

foo foo::bar = new foo;
try foo* foo::bar = new foo;

Will instance create a copy of the object when it's dereferenced?
it depends... you are returning an object by reference in the function
instance().
foo f = foo::instance(); // will create a copy
foo& rf = foo::instance(); // no copy is created

Thanks,

--John Ratliff

try something like that:
static foo* getInstance() { if (!bar) bar = new foo; return bar; };
.....
foo* foo::bar = 0;
....

int main()
{
foo* f = foo::getInstance();
}
Greets Chris
Aug 10 '05 #2
> Will instance create a copy of the object when it's dereferenced?

The dereference operation itself does not copy the object. However, the
result of the operation is likely to be used for a copy construction.

foo& ref = foo::instance(); // ref is another name of *(foo::bar)
foo a = foo::instance(); // copy constructing a

Ben
Aug 10 '05 #3
Christian Meier wrote:
"John Ratliff" <us**@example.net> schrieb im Newsbeitrag
news:j8kKe.236394$x96.154050@attbi_s72...
When I dereference a pointer, does it make a copy of the object?

Say I had a singleton, and wanted an static method to retrieve it from
the class.

class foo {
private:
static foo *bar;

foo() {} // no public creation!
public:
~foo() { delete bar; }
foo &instance() { return *bar; }

IMHO instance should be a static function.


Apologies. I quickly typed an example. In my actual class, instance is a
static function. I also just realized the destructor is deleting bar,
which would be very bad if the object were copy constructed. Should be
delete this, or have an overridden undefined copy constructor.
};

foo foo::bar = new foo;

try foo* foo::bar = new foo;


Typo again here.
try something like that:
static foo* getInstance() { if (!bar) bar = new foo; return bar; };
....
foo* foo::bar = 0;
...

int main()
{
foo* f = foo::getInstance();
}


I'd prefer to return a reference since no copy is created.

I also thought about the null object pattern, but the overhead of
creating the object at startup is less than the overhead of the if tests.

However, I have a new (but related) problem. Let's say I have another
class that uses this singleton extensively. There are multiple instances
of this class running around (5 to be precise). Multiple functions in
this class will need to use this singleton. What happens if I try to
include a static reference to the singleton in the multiple-instance
class? Something like this:

class a_singleton {
private:
static a_singleton *singleton;

a_singleton() {} // singleton!

public:
a_singleton(const a_singleton &); // singleton
~a_singleton() { delete this; }
static &a_singleton instance() { return *singleton; }
};

a_singleton *a_singleton::singleton = new a_singleton;

class b_5instancer {
private:
static a_singleton &singleton;
}

a_singleton &b_5instancer::singleton = a_singleton::instance();

Is this possible? Two questions spring to mind.

a) can you have a static class member reference? If not, I suppose it
could be a pointer.

b) Is there any way to know which would be initialized first. It seems
like unless I used the null object pattern, there's a good chance I
could be dereferencing an unknown (very possibly uninstantiated)
pointer to a_singleton when calling instance() during runtime
creation.

If this is not possible, would it work with a static reference (or
pointer if static member references are not possible) if a_singleton
used the null object pattern?

Thanks,

--John Ratliff
Aug 10 '05 #4

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

Similar topics

10
by: fabio de francesco | last post by:
Hi what do you think of the following? Why are we permitted to do that? And why the C++ Library doesn't stop someone willing to perfom that assignement (*a = 20)? #include <iostream> ...
51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
1
by: Vam | last post by:
dereferencing a pointer to base always provides a base-class slice even if the object pointed to was of a derived type, correct?
6
by: Shankar | last post by:
Hello, We have a smart pointer class which provides the dereference operator -> to access the underlying object pointer. Now, we have a new requirement where a different type of object (e.g from...
4
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try...
28
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
4
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.