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

smart ptrs in vectors

er
hi,

i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<Avec;
vec.push_back(a0);

if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print. any suggestion appreciated. thanks!

class Impl{
public:
Impl(unsigned int i_):i(i_){};
private:
unsigned int i;
};
class A{
public:
A(Impl& impl_):impl(impl_){};
A(const A& a):impl(a.impl){std::cout<<"A::A(const
A&)"<<std::endl;};
A& operator=(const A& a);
A& operator=(const A& a);{//complain if private
std::cout<<"A::operator="<<std::endl;//does not show
if(&a!=this){
throw
std::runtime_error("A::operator=");
};
return *this;
};
private:
A();
Impl& impl;//Impl& instead of some_ptr<Implbecause i intend to
keep impl throughout lifetime of A. anything wrong with it?
};

Sep 17 '07 #1
5 1158
er
On Sep 17, 7:15 pm, er <erwann.rog...@gmail.comwrote:
hi,

i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<Avec;
vec.push_back(a0);

if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print. any suggestion appreciated. thanks!

class Impl{
public:
Impl(unsigned int i_):i(i_){};
private:
unsigned int i;};

class A{
public:
A(Impl& impl_):impl(impl_){};
A(const A& a):impl(a.impl){std::cout<<"A::A(const
A&)"<<std::endl;};
A& operator=(const A& a);
A& operator=(const A& a);{//complain if private
std::cout<<"A::operator="<<std::endl;//does not show
if(&a!=this){
throw
std::runtime_error("A::operator=");
};
return *this;
};
private:
A();
Impl& impl;//Impl& instead of some_ptr<Implbecause i intend to
keep impl throughout lifetime of A. anything wrong with it?

};
ps: ignore the first A& operator=(const A& a)
Sep 17 '07 #2
er wrote:
hi,

i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<Avec;
vec.push_back(a0);

if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print. any suggestion appreciated. thanks!
I guess that's specific implementation like VC,
And I guess it use placement new to copy the new 'push_back'ed element,
that's why copy ctor is called.

Though not using A::operator=, but STL require element of container to
be Assignable, so I also guess it borrow some concept check.

All my guess.
>
class Impl{
public:
Impl(unsigned int i_):i(i_){};
private:
unsigned int i;
};
class A{
public:
A(Impl& impl_):impl(impl_){};
A(const A& a):impl(a.impl){std::cout<<"A::A(const
A&)"<<std::endl;};
A& operator=(const A& a);
A& operator=(const A& a);{//complain if private
std::cout<<"A::operator="<<std::endl;//does not show
if(&a!=this){
throw
std::runtime_error("A::operator=");
};
return *this;
};
private:
A();
Impl& impl;//Impl& instead of some_ptr<Implbecause i intend to
keep impl throughout lifetime of A. anything wrong with it?
Using *Pimpl* idiom (I think you attempt to use it), it's more
straightforward to use pointer, as you don't initialize to the value
(you don't even know what kind of impl to attach); Using pimpl is always
trying to hide implementation, so you use forward declaration for *impl*
rather than a definition of 'impl'.

using reference you have to initialize the reference when you initialize
your 'obj' who wraps it. So you have to garantee the lifetime of the
'impl', which have to live longer than your 'obj'.

So the idiom is *P*ointer *Impl*ementation.
};

--
Thanks
Barry
Sep 18 '07 #3
On Sep 18, 1:15 am, er <erwann.rog...@gmail.comwrote:
i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<Avec;
vec.push_back(a0);
if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print.
The standard says that any object put into a standard container
must be assignable. That doesn't mean that the assignment
operator will be called for all operations on the container.

In this case, what is probably happening is that the
implementation of vector<>::push_back calls vector<>::insert.
And in certain cases (but never when inserting at the end),
insert must use the assignment operator. The reason you get the
compiler error is because the compiler is instantiating a
function which uses it. The reason you never see its output is
because it is in an if/else branch which never actually gets
executed.

Note that some implementations of the library use concept
checking, and you may get an error from the compiler any time
you instantiate the template, e.g. in the declaration, even if
you never actually do anything with the resulting instantiation.
any suggestion appreciated.
Provide a working assignment operator for all objects which are
to be in a standard container.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 18 '07 #4
James Kanze wrote:
On Sep 18, 1:15 am, er <erwann.rog...@gmail.comwrote:
>i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<Avec;
vec.push_back(a0);
>if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print.

The standard says that any object put into a standard container
must be assignable. That doesn't mean that the assignment
operator will be called for all operations on the container.
Yes and the standard also says the objects must be copy constructable.
The copy constructor obviously is what is moving the object into the
container.
Sep 18 '07 #5
er
On Sep 18, 7:58 am, Ron Natalie <r...@spamcop.netwrote:
James Kanze wrote:
On Sep 18, 1:15 am, er <erwann.rog...@gmail.comwrote:
i'm wondering what is the role of A::operator= below in doing:
Impl impl0(0);
A a0(impl0);
std::vector<Avec;
vec.push_back(a0);
if i put it as private the compiler complains but if i put it as
public, the message "A::operator=" does not appear. the message
"A::A(A&)" however does print.
The standard says that any object put into a standard container
must be assignable. That doesn't mean that the assignment
operator will be called for all operations on the container.

Yes and the standard also says the objects must be copy constructable.
The copy constructor obviously is what is moving the object into the
container.
thank you all. it now makes sense. yes i'm aware this is a kind of
pimpl that uses a reference instead of a ptr (i call it rimpl), but
with the benefit your suggestions, it's probably safer to use a ptr.

Sep 18 '07 #6

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

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
13
by: christopher diggins | last post by:
What I am trying to answer is, what the most prominent motivation for implenting and using smart pointers in C++ is. Is it primarily to reduce bugs resulting from memory access errors or primarily...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
1
by: jon wayne | last post by:
Hi Am trying to replace the classic switch case construct (am workng on a telecom stack devlpmnt)with an array of function ptrs. The problm am facing is that of indexing. - the case vals can be...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
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...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
3
by: Tim H | last post by:
I'm wondering if there is a way to do this properly. I feel like I am missing something. I have two distinct classes. They don't have much at all in common. I want a vector-like container that...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.