Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Constructor question... (http://www.velocityreviews.com/forums/t957569-constructor-question.html)

TDH1978 02-13-2013 01:29 AM

Constructor question...
 
I have a class where the default constructor is disabled, and the only
constructor takes one parameter. The construction of the object
depends on a condition:

if (cond)
A a(x);
else
A a(y);

The problem is that the object 'a' goes out of scope after the 'if'
statement, and I do not want to use pointers, like:

A* aptr;
if (cond)
aptr = new A(x)
else
aptr = new A(y);


Is there a more elegant solution?


Luca Risolia 02-13-2013 02:20 AM

Re: Constructor question...
 
On 13/02/2013 02:29, TDH1978 wrote:
> I have a class where the default constructor is disabled, and the only
> constructor takes one parameter. The construction of the object depends
> on a condition:
>
> if (cond)
> A a(x);
> else
> A a(y);
>
> The problem is that the object 'a' goes out of scope after the 'if'
> statement, and I do not want to use pointers, like:
>
> A* aptr;
> if (cond)
> aptr = new A(x)
> else
> aptr = new A(y);
>
>
> Is there a more elegant solution?
>


You did not define A and the type of cond, x and y, so it's not possible
to give you a general answer. Anyway, see if A a(cond ? x : y) works for
you.


Öö Tiib 02-13-2013 07:59 AM

Re: Constructor question...
 
On Wednesday, 13 February 2013 03:29:15 UTC+2, TDH1978 wrote:
> I have a class where the default constructor is disabled, and the only
> constructor takes one parameter. The construction of the object
> depends on a condition:


Better write that also in C++, English is too ambiguous to forward lot
of (possibly important) details.

> if (cond)
> A a(x);
> else
> A a(y);
>
> The problem is that the object 'a' goes out of scope after the 'if'
> statement, and I do not want to use pointers, like:
>
> A* aptr;
> if (cond)
> aptr = new A(x)
> else
> aptr = new A(y);
>
> Is there a more elegant solution?


Multitude. For example separate deciding the argument and constructing a:

AParamType arg = cond ? x : y;

A a(arg);

Fred Zwarts \(KVI\) 02-13-2013 08:20 AM

Re: Constructor question...
 
"TDH1978" wrote in message news:kfeq82$ekg$1@dont-email.me...
>
>I have a class where the default constructor is disabled, and the only
>constructor takes one parameter. The construction of the object depends on
>a condition:
>
> if (cond)
> A a(x);
> else
> A a(y);
>
>The problem is that the object 'a' goes out of scope after the 'if'
>statement, and I do not want to use pointers, like:
>
> A* aptr;
> if (cond)
> aptr = new A(x)
> else
> aptr = new A(y);
>
>
>Is there a more elegant solution?


A a (cond ? x : y);


Adam Wysocki 02-13-2013 11:11 AM

Re: Constructor question...
 
TDH1978 <thedeerhunter1978@movie.uni> wrote:

> if (cond)
> A a(x);
> else
> A a(y);


Try:

A a(cond ? x : y);

Note that x and y have to be same type, so this will not work:

class B {};
class D1: public B {};
class D2: public B {};

class A
{
public:
A(B) {}
};

void fn(int i)
{
D1 d1;
D2 d2;
A a(i == 5 ? d1 : d2);
}

AW

Adam Wysocki 02-13-2013 11:12 AM

Re: Constructor question...
 
TDH1978 <thedeerhunter1978@movie.uni> wrote:

> The problem is that the object 'a' goes out of scope after the 'if'
> statement, and I do not want to use pointers, like:


In other situations and more complex conditions, when you don't want to
use ?: operator, you can consider using auto_ptr.

AW

Tobias Müller 02-13-2013 12:30 PM

Re: Constructor question...
 
Andy Champ <no.way@nospam.invalid> wrote:
> On 13/02/2013 02:20, Luca Risolia wrote:
>> On 13/02/2013 02:29, TDH1978 wrote:
>>> I have a class where the default constructor is disabled, and the only
>>> constructor takes one parameter.

<snip>
>>> Is there a more elegant solution?

<snip>
>> Anyway, see if A a(cond ? x : y) works for you.

>
> This assumes that there is only one constructor for A.


That's exactly what the OP said.

Tobi

Tobias Müller 02-13-2013 12:54 PM

Re: Constructor question...
 
Adam Wysocki <gof@somewhere.invalid> wrote:
> TDH1978 <thedeerhunter1978@movie.uni> wrote:
>
>> if (cond)
>> A a(x);
>> else
>> A a(y);

>
> Try:
>
> A a(cond ? x : y);
>
> Note that x and y have to be same type,


That's not true. Essentially, it works if one of the two is convertible to
the type (or a reference of the type) of the other.
(It's actually a bit more complex and sometimes the result is a temporary)

> so this will not work:
>
> class B {};
> class D1: public B {};
> class D2: public B {};
>
> class A
> {
> public:
> A(B) {}
> };
>
> void fn(int i)
> {
> D1 d1;
> D2 d2;
> A a(i == 5 ? d1 : d2);
> }


A a(i == 5 ? (B&)d1 : d2); // Now it works

Tobi

Adam Wysocki 02-19-2013 01:59 PM

Re: Constructor question...
 
Tobias Müller <troplin@bluewin.ch> wrote:

> That's not true. Essentially, it works if one of the two is convertible to
> the type (or a reference of the type) of the other.


Thanks for the clarification.

AW

Anand Hariharan 02-19-2013 06:27 PM

Re: Constructor question...
 
On Feb 13, 6:30*am, Tobias Müller <trop...@bluewin.ch> wrote:
> Andy Champ <no....@nospam.invalid> wrote:
> > On 13/02/2013 02:20, Luca Risolia wrote:
> >> On 13/02/2013 02:29, TDH1978 wrote:
> >>> I have a class where the default constructor is disabled, and the only
> >>> constructor takes one parameter.

> <snip>
> >>> Is there a more elegant solution?

> <snip>
> >> Anyway, see if A a(cond ? x : y) works for you.

>
> > This assumes that there is only one constructor for A.

>
> That's exactly what the OP said.
>


The expression involving ternary operator requires that 'x' and 'y' be
of the same type.

Luca Risolia is right in that the OP hasn't provided sufficient
information to give an accurate answer.



All times are GMT. The time now is 09:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.