C++: how to pass a variable as an argument to class constructor -


i faced puzzling issue. trying construct object using variable parameter.

have @ code please:

#include <iostream>  class {    public:     a()      : val(0) {};     a(int v) : val(v) {};    private:     int val; };  main() {    int number;    a;       /* good, object of type a, created without arguments */   b(2);    /* good, object of type a, created 1 argument */   c();     /* bad, c++ thinks declaration of                 function returning class type */   a(d);      /* good, object of type again; no arguments */   a(number); /* bad, tries re-declare "number" object of type */ } 

i think understand why objects "a", "b" , "d" can created, whereas rest can not.

but need syntax of last declaration, i.e.

    a(number); 

to create temporary object, pass argument object.

any idea how work around it?

kind regards 

your intention create temporary object, object would anonymous : have no way refer after semicolon. there no point in creating anonymous temporary object discard it. have instantiate temporary directly @ call site of ctor/method consume it.

solution

to pass anonymous temporary object function, need instantiate inside arguments' list :

functionexpectinga(a(number)); 

for line of "c declaration", poking @ most vexing parse. if need pass default constructed a object argument object constructor, need add pair of braces trick (so compiler can distinguish function declaration) :

class otherclass { public:     otherclass(a a)     {          //...     }; };  otherclass obj((a()));                ^   ^ 

edit #1 : jrok pointed out argument given a constructor not enough resolve ambiguity.

if need pass anonymous temporary object built argument, there no ambiguity (so no need parentheses), still need parentheses around anonymous object construction.:

otherclass obj((a(number))); 

c heritage : single argument not enough

edit #2 : "why giving single argument constructor not resolve ambiguity".

what happens otherclass obj(a(number)); ?
declaration function named obj, taking a object unique argument. argument named number.
i.e: equivalent otherclass obj(a number);. of course, can omit argument name @ function declaration. sementically equivalent otherclass obj(a);

the syntax parentheses around object name inherited c :

int(a); // declares (and defines) int. int a;  // strictly equivalent. 

what more arguments constructor ?

if added ctor otherclass taking 2 (or more) arguments :

    otherclass(a a1, a2)     {          //...     }; 

then syntax :

a arg1, arg2; otherclass obj(a(arg1, arg2)); 

would time declare obj instance of otherclass.
because a(arg1, arg2) cannot interpreted name declaration a. parsed construction of anonymous a object, using constructor 2 parameters.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -