468,458 Members | 1,846 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

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 2491
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Susan Baker | last post: by
7 posts views Thread by Paminu | last post: by
92 posts views Thread by Jim Langston | last post: by
14 posts views Thread by Ian | last post: by
33 posts views Thread by Ney André de Mello Zunino | last post: by
4 posts views Thread by Deep | last post: by
13 posts views Thread by WaterWalk | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.