prevent assignement from a vector

M

mosfet

Hi,

In one my class I would like to give access to one of my buffer so I
have declared something like this :

vector<char>& RespData = pWebResp->get_RawBuffer();

The problem is caller can also write this :

vector<char> RespData = pWebResp->get_RawBuffer();

and in this case will it copy my buffer into RespData ?
If yes how can I prevent this behavior ?
 
V

Victor Bazarov

mosfet said:
In one my class I would like to give access to one of my buffer so I
have declared something like this :

vector<char>& RespData = pWebResp->get_RawBuffer();

You "have declared" something like this? What does that mean? Are
you telling us you have 'get_RawBuffer' that returns a reference to
non-const vector? OK, then.
The problem is caller can also write this :

vector<char> RespData = pWebResp->get_RawBuffer();

and in this case will it copy my buffer into RespData ?

Yes, it will.
If yes how can I prevent this behavior ?

WHY?

There is no way for you do prevent copying unless you use some kind
of class (instead of 'vector<char>') that does not have public copy
semantics defined (has them private, for example).

V
 
N

Noah Roberts

mosfet said:
Hi,

In one my class I would like to give access to one of my buffer so I
have declared something like this :

vector<char>& RespData = pWebResp->get_RawBuffer();

The problem is caller can also write this :

vector<char> RespData = pWebResp->get_RawBuffer();

and in this case will it copy my buffer into RespData ?
If yes how can I prevent this behavior ?

Create a class "data_reference" that wraps up your vector in an
interface that only implements those calls to vector you wish the client
to make. All it does is call the appropriate function in the vector,
which it contains a pointer to. This class will obviously not be
assignable to a vector since it won't be a related type.
 
B

Barry

mosfet said:
Hi,

In one my class I would like to give access to one of my buffer so I
have declared something like this :

vector<char>& RespData = pWebResp->get_RawBuffer();

I guess that you have a member of vector<char> in pWebResp.
assume that pWebResp is of WebResp* type;
then you can let /WebResp/ non-copyable by declare /operator=/ and
copy-ctor private members of /WebResp/.
The problem is caller can also write this :

vector<char> RespData = pWebResp->get_RawBuffer();

Oh, then you have to use a wrapper for std::vector<char>
class NonCopyVector
: public std::vector<char>
{
private:
NonCopyVector(NonCopyVector const&);
NonCopyVector& operator= (NonCopyVector const&);
};

I guess someone will say, don't inherit from vector.

OK,
but why bot just use

vector<char>* pResp = pWebResp->get_RawBuffer();
// return pointer then reference

If you still likes return by reference

OK,

class VectorRef
{
public:
typedef std::vector<char>& ref_type;
VectorRef(ref_type ref) : ref_(ref) {}
operator ref_type() { return ref_; }
private:
ref_type ref_;
};

then
VectorRef dataRef = pWebResp->get_RawBuffer();

but you have to explicit cast to std::vector<char>& type before calling
its member functions

(static_cast<std::vector<char>&>(dataRef)).begin();

/////////////////////

IMO, change to return pointer is better.
 
M

mosfet

Barry a écrit :
I guess that you have a member of vector<char> in pWebResp.
assume that pWebResp is of WebResp* type;
then you can let /WebResp/ non-copyable by declare /operator=/ and
copy-ctor private members of /WebResp/.


Oh, then you have to use a wrapper for std::vector<char>
class NonCopyVector
: public std::vector<char>
{
private:
NonCopyVector(NonCopyVector const&);
NonCopyVector& operator= (NonCopyVector const&);
};

I guess someone will say, don't inherit from vector.

OK,
but why bot just use

vector<char>* pResp = pWebResp->get_RawBuffer();
// return pointer then reference

If you still likes return by reference

OK,

class VectorRef
{
public:
typedef std::vector<char>& ref_type;
VectorRef(ref_type ref) : ref_(ref) {}
operator ref_type() { return ref_; }
private:
ref_type ref_;
};

then
VectorRef dataRef = pWebResp->get_RawBuffer();

but you have to explicit cast to std::vector<char>& type before calling
its member functions

(static_cast<std::vector<char>&>(dataRef)).begin();

/////////////////////

IMO, change to return pointer is better.
Thanks a lot for this full answer!!!
 
T

tony_in_da_uk

vector<char>& RespData = pWebResp->get_RawBuffer();

The problem is caller can also write this :

vector<char> RespData = pWebResp->get_RawBuffer();

Why not just return a pointer to the vector? If the first form is to
be valid, then of course they could copy the vector if they make a
little effort. So, you're trying to hint, or make it easier or more
natural to access the data at the address provided by pWebResp. A
pointer's a pretty clear fit.

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top