Objects can't be passed by value

M

Manish

How to construct a class such that it's objects can't be "Passed by
Value?"

Can anyone shed some light on this?
 
A

Alf P. Steinbach

* Manish:
How to construct a class such that it's objects can't be "Passed by
Value?"

Can anyone shed some light on this?

Remove access to copy construction.

Quite possibly this is also a FAQ.

Read the FAQ, please.
 
M

Mike Wahler

Manish said:
How to construct a class such that it's objects can't be "Passed by
Value?"

Can anyone shed some light on this?

class C
{
private:
C(const C&);
};

Is this not addressed in your C++ text?

-Mike
 
M

Manish

Mike said:
class C
{
private:
C(const C&);
};

Is this not addressed in your C++ text?

-Mike

hmm... seems like a simple enough answer. Or perhaps, I didn't pose my
question correctly, so please excuse my ignorance.

class base {
.
.
.
};

class derived: base {
do_something(base xyz); <-- can this be restricted?
};

int main ()
{
derived dr;
base ba;

dr.do_something(ba);
return 0;
}

Thanks a bunch in advance.

Regards,
Manish
 
M

Mike Wahler

Manish said:
hmm... seems like a simple enough answer.

It was a simple question. :)
Or perhaps, I didn't pose my
question correctly, so please excuse my ignorance.

class base {
.
.
.
};

class derived: base {
do_something(base xyz); <-- can this be restricted?
};

int main ()
{
derived dr;
base ba;

dr.do_something(ba);
return 0;
}

class base
{
public:
base(){}

private:
base(const base&);
};

class derived : base
{
public:
void do_something(base xyz){}
};

int main ()
{
derived dr;
base ba;

dr.do_something(ba); // line 21
return 0;
}

Compile...

error (line 21): 'base::base' : cannot access private member declared in
class 'base'

-Mike
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top