
Hello all, I'm trying to use ptr_set with my class Hierarchy. Base is an abstract base class with a bunch of pure virtual functions. I've some derived classes with those virtual functions defined in them. I'm using this Base class hierarchy in another class (say Other). So I've declared a member variable of Other as ptr_set<Base> m_items ; this works fine on linux with gcc-4.2 but with VS2005, I'm getting a compilation error, saying that It cannot instantiate base class as it is having pure virtual functions. Am I doing some thing wrong here ? Surya

On Mon, May 19, 2008 at 9:37 AM, Surya Kiran Gullapalli < suryakiran.gullapalli@gmail.com> wrote:
Hello all, I'm trying to use ptr_set with my class Hierarchy.
Base is an abstract base class with a bunch of pure virtual functions.
I've some derived classes with those virtual functions defined in them.
I'm using this Base class hierarchy in another class (say Other).
So I've declared a member variable of Other as
ptr_set<Base> m_items ;
this works fine on linux with gcc-4.2 but with VS2005, I'm getting a compilation error, saying that It cannot instantiate base class as it is having pure virtual functions.
Am I doing some thing wrong here ?
Surya
The Base Class I'm talking about is derived from boost::noncopyable, with a protected Copy Constructor

Surya Kiran Gullapalli skrev:
On Mon, May 19, 2008 at 9:37 AM, Surya Kiran Gullapalli
mailto:suryakiran.gullapalli@gmail.com> wrote: Hello all, I'm trying to use ptr_set with my class Hierarchy.
Base is an abstract base class with a bunch of pure virtual functions.
I've some derived classes with those virtual functions defined in them.
I'm using this Base class hierarchy in another class (say Other).
So I've declared a member variable of Other as
ptr_set<Base> m_items ;
this works fine on linux with gcc-4.2 but with VS2005, I'm getting a compilation error, saying that It cannot instantiate base class as it is having pure virtual functions.
Am I doing some thing wrong here ?
Surya
The Base Class I'm talking about is derived from boost::noncopyable, with a protected Copy Constructor
I need more info than that to figure it out. -Thorsten

I'm still trying to figure out why I'm getting this wierd error. When i isolated the code and compiled, the compilation went fine with out any problem. Also, If i declare the ptr_set variable in the header file as static, then there's no compilation error. Any ideas. I know it'd be help ful to reproduce the problem with a small sample code, which I'm trying to do. Surya

hello all, One little question: is it possible to make a class assignable which has reference type members? Thanks for your interest and for any hints or help. B/Rgds Max

I'm pretty sure reference members can only be initialised on construction -
so if your class has reference members you can have a copy constructor, but
not an assignment operator.
However, have a look at Boost.Ref which allows you to wrap references in a
copyable object. This might be what you're after.
Alex
On 19/05/2008, Max
hello all,
One little question: is it possible to make a class assignable which has reference type members?
Thanks for your interest and for any hints or help.
B/Rgds Max
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

----- Original Message -----
From: "Max"
hello all,
One little question: is it possible to make a class assignable which has reference type members?
Hello Max, yes it is possible. Just assign the reference member of the rhs to the the reference member of the lhs. The single feature of references is that they are no default constructible. Maybe your question was? is it possible to make a class which has reference type members default constructible? Vicente Juan Botet Escriba

hello all,
One little question: is it possible to make a class assignable which has reference type members?
Hello Max,
yes it is possible. Just assign the reference member of the rhs to the the reference member of the lhs.
The single feature of references is that they are no default constructible. Maybe your question was? is it possible to make a class which has reference type members default constructible?
Vicente Juan Botet Escriba
I think he wanted the references to be copied the way pointers would be; i.e after A = B, A refers to the same objects B does (not a copy). I dont think you can change reference to refer to another object once it is initialized (sec 8.5.3 par. 2). I wonder if you can use placement new and make the assignment operator for your class invoke a copy constructor? -- John

----- Original Message -----
From: "John Femiani"
hello all,
One little question: is it possible to make a class assignable which has reference type members?
Hello Max,
yes it is possible. Just assign the reference member of the rhs to the the reference member of the lhs.
I think he wanted the references to be copied the way pointers would be; i.e after A = B, A refers to the same objects B does (not a copy).
I dont think you can change reference to refer to another object once it is initialized (sec 8.5.3 par. 2). I wonder if you can use placement new and make the assignment operator for your class invoke a copy constructor?
I'm really sorry. I don't know what I was thinking on. Vicente

AMDG John Femiani wrote:
I wonder if you can use placement new and make the assignment operator for your class invoke a copy constructor?
Don't do that. It is impossible to make such an assignment operator exception safe if the copy constructor can throw. In Christ, Steven Watanabe

Steven Watanabe wrot:
John Femiani wrote:
I wonder if you can use placement new and make the assignment operator for your class invoke a copy constructor?
Don't do that. It is impossible to make such an assignment operator exception safe if the copy constructor can throw.
I thought it would be a risky maneuver :) Perhaps the answer for Max is "it may be possible, but its probably not wise" -- John

Hello Max,
yes it is possible. Just assign the reference member of the rhs to the the reference member of the lhs.
The single feature of references is that they are no default constructible. Maybe your question was? is it possible to make a class which has reference type members default constructible?
Vicente Juan Botet Escriba
Hello Escriba Thank you for your clarification. I just didn't know the fact that ref type member var's could be assigned using '=' before you told me. However, I'm also curious on the question you raised for me: :-) Is it possible to make a class which has reference type members default constructible? AFAIK, the answer is no. I hope and don't hope there's a different answer. Thanks and best regards Max

On Monday 19 May 2008 21:30, Max wrote:
Thank you for your clarification.
I just didn't know the fact that ref type member var's could be assigned using '=' before you told me.
It sounds like you'd be assigning the underlying variable the references were initialized with, but you want is to make the reference point at a different variable?
However, I'm also curious on the question you raised for me: :-)
Is it possible to make a class which has reference type members default constructible?
AFAIK, the answer is no. I hope and don't hope there's a different answer.
It sounds like you need to be using pointers instead of references.

Thank you for your clarification.
I just didn't know the fact that ref type member var's could be assigned using '=' before you told me.
It sounds like you'd be assigning the underlying variable the references were initialized with, but you want is to make the reference point at a different variable?
Your are right and it's my fault. I should have had a trial-and-test before posting a reply. :-( To make it clearer, I'm finding something like this: class Foo { int & i_; Foo& operator=(const Foo& rhs) { if(&rhs == this) return *this; i_ = rhs.i_; return *this; } } this is not accepted by my VC9 compiler.
However, I'm also curious on the question you raised for me: :-)
Is it possible to make a class which has reference type members default constructible?
AFAIK, the answer is no. I hope and don't hope there's a different answer.
It sounds like you need to be using pointers instead of references.
Yes, it seems to be the only way, as far as I don't to make thing more complex by using another layer of indirectness. Thanks for your time B/Rgds Max

AMDG Max wrote:
To make it clearer, I'm finding something like this: class Foo { int & i_; Foo& operator=(const Foo& rhs) { if(&rhs == this) return *this; i_ = rhs.i_; return *this; } }
this is not accepted by my VC9 compiler.
It isn't? I don't see a problem (other than that it may not be what you intend), and I just compiled the above snippet with vc9.
However, I'm also curious on the question you raised for me: :-)
Is it possible to make a class which has reference type members default constructible?
AFAIK, the answer is no. I hope and don't hope there's a different answer.
It sounds like you need to be using pointers instead of references.
Yes, it seems to be the only way, as far as I don't to make thing more complex by using another layer of indirectness.
Well, the indirectness is still there with references. It's just hidden. In Christ, Steven Watanabe

Max wrote:
To make it clearer, I'm finding something like this: class Foo { int & i_; Foo& operator=(const Foo& rhs) { if(&rhs == this) return *this; i_ = rhs.i_; return *this; } }
this is not accepted by my VC9 compiler.
It isn't? I don't see a problem (other than that it may not be what you intend),
Oops, It's indeed not my intention. What I wanted is to bind i_ to rhs.i_; Thanks and B/Rgds. Max

Max wrote:
To make it clearer, I'm finding something like this: class Foo { int & i_; Foo& operator=(const Foo& rhs) { if(&rhs == this) return *this; i_ = rhs.i_; return *this; } }
this is not accepted by my VC9 compiler.
It isn't? I don't see a problem (other than that it may not be what you intend),
Oops, It's indeed not my intention. What I wanted is to bind i_ to rhs.i_;
Hi Max, First, the C++ standard does not allow a reference to change which object it refers to after it has been initialized. Second, I'm just curious, why do you want to do this? Why don't you use a pointer? Or perhaps wrap a pointer in a class with a typcast operator that produces a reference, if you need it to have the same syntax? Something like this: template<class T> class assignable_ref{ private: T* _pointer; public: explicit assignable_ref(T& value) :_pointer(&value) {} operator T&() const { return *_pointer; } assignable_ref& operator=(T& rhs) { _pointer = &rhs; return *this; } assignable_ref& operator=(assignable_ref<T> const & rhs) { _pointer = rhs._pointer; return *this; } }; class Foo { private: assignable_ref<int> i_; public: Foo(int& value) : i_(value){}; Foo& operator=(const Foo& rhs) { if(&rhs == this) return *this; i_ = rhs.i_; return *this; } }; -- John

John Femiani wrote:
Max wrote:
To make it clearer, I'm finding something like this: class Foo { int & i_; Foo& operator=(const Foo& rhs) { if(&rhs == this) return *this; i_ = rhs.i_; return *this; } }
this is not accepted by my VC9 compiler.
It isn't? I don't see a problem (other than that it may not be what you intend), Oops, It's indeed not my intention. What I wanted is to bind i_ to rhs.i_;
Hi Max, First, the C++ standard does not allow a reference to change which object it refers to after it has been initialized. Second, I'm just curious, why do you want to do this? Why don't you use a pointer? Or perhaps wrap a pointer in a class with a typcast operator that produces a reference, if you need it to have the same syntax?
Just my two cents, but I would definitely go with the wrapper option or use a shared_ptr. The other option requires that you track which of the objects will own the object pointed to and will delete it. Usually you want this to be the last one anyway so a shared_ptr is very appropriate here. Doing this tracking by hand can be a lot of work and there's room in there for a lot of problems. The amount of care needed in making sure that everyone knows the details about how this class works often exceeds the care you think is necessary. I've seen some pretty serious bugs come out of classes that 'sometimes' shared a pointer with other instances of the same class. This is a perfect time for a shared_ptr or other wrapper object to encapsulate all that crap.
participants (9)
-
Alex MDC
-
Frank Mori Hess
-
John Femiani
-
Max
-
Noah Roberts
-
Steven Watanabe
-
Surya Kiran Gullapalli
-
Thorsten Ottosen
-
vicente.botet