By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Initializing a smart pointer

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a

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

P: n/a

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.