472,805 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Initializing a smart pointer

class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}

private:
smart_pointer<Somethingsmart_ptr_;

};
As you can (hopefully) see, I've got a need to dynamically create an
object whose constructor needs some parameters that I can only get at
runtime.

Is the above way the best way to do it? Seems a bit clunky.

Thanks,
Joe
Jul 6 '06 #1
5 2668
Joe Van Dyk wrote:
class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}

private:
smart_pointer<Somethingsmart_ptr_;

};
As you can (hopefully) see, I've got a need to dynamically create an
object whose constructor needs some parameters that I can only get at
runtime.

Is the above way the best way to do it? Seems a bit clunky.
No more clunky than Something* p = new Something(param);

--
Ian Collins.
Jul 6 '06 #2
Joe Van Dyk wrote:
class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}

private:
smart_pointer<Somethingsmart_ptr_;

};
As you can (hopefully) see, I've got a need to dynamically create an
object whose constructor needs some parameters that I can only get at
runtime.

Is the above way the best way to do it? Seems a bit clunky.
IMHO, it's probably better to separate the "obtaining data" and
"creating Foo".

e.g.

class Foo {
public:
Foo(int param) : smart_ptr_(new Something(param))
{
}
private:
smart_pointer<Somethingsmart_ptr_;
};

void f()
{
int param = read_some_data();
Foo foo(param);
}
Jul 6 '06 #3

Joe Van Dyk wrote:
class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}

private:
smart_pointer<Somethingsmart_ptr_;

};
As you can (hopefully) see, I've got a need to dynamically create an
object whose constructor needs some parameters that I can only get at
runtime.

Is the above way the best way to do it? Seems a bit clunky.
You can move the constructor logic to another method, which you can
then call on the initialize list.
Example:
class Foo
{
public:
Foo() : smart_ptr_( MakeSomething() )
{
}

static Something* MakeSomething()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
return new Something(param);
}

private:
smart_pointer<Somethingsmart_ptr_;
};

In above example, I makde the method static, so as to avoid accessing
the object's data members. Any method you call in the initialize list
should avoid accessing the data members, because the Foo object is not
yet formed.

Jul 7 '06 #4

Joe Van Dyk wrote:
class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}

private:
smart_pointer<Somethingsmart_ptr_;

};
As you can (hopefully) see, I've got a need to dynamically create an
object whose constructor needs some parameters that I can only get at
runtime.

Is the above way the best way to do it? Seems a bit clunky.

Thanks,
Joe
private:
smart_pointer<Somethingsmart_ptr_;
smar_ptr_ is not a pointer. it is an object. It has already been
instantiated.
How can you use new on that?

could be like this..

smart_pointer<Something*smart_ptr_;
smart_ptr_ = smart_pointer<Something>(new Something(param));
you are trying to create an object or type 'Something' where the LValue
is of type 'smart_pointer<Something>'

you can write that statement as follows.. (if *smart_ptr_)

smart_ptr_ = new smart_pointer<Something>(param);

you may ask how to pass the value of param to the construtor of class
'something'.

you have to deal it in the definition of the 'Something' constructor.

-- Murali Krishna

Jul 7 '06 #5

Murali Krishna wrote:
Joe Van Dyk wrote:
class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}

private:
smart_pointer<Somethingsmart_ptr_;

};
As you can (hopefully) see, I've got a need to dynamically create an
object whose constructor needs some parameters that I can only get at
runtime.

Is the above way the best way to do it? Seems a bit clunky.

Thanks,
Joe
private:
smart_pointer<Somethingsmart_ptr_;

smar_ptr_ is not a pointer. it is an object. It has already been
instantiated.
How can you use new on that?

could be like this..

smart_pointer<Something*smart_ptr_;
smart_ptr_ = smart_pointer<Something>(new Something(param));

you are trying to create an object or type 'Something' where the LValue
is of type 'smart_pointer<Something>'

you can write that statement as follows.. (if *smart_ptr_)

smart_ptr_ = new smart_pointer<Something>(param);

you may ask how to pass the value of param to the construtor of class
'something'.

you have to deal it in the definition of the 'Something' constructor.

-- Murali Krishna
Sorry in 'smart_pointer' constructor

-- Murali Krishna

Jul 7 '06 #6

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
92
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,...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.