views:

81

answers:

3

Suppose we have following two classes:

class Temp{
 public:
  char a;
  char b;
};
class Final{
 private:
  int a;
  char b;
  char c;
 public:
  Final(Temp in):b(in.a),c(in.b){}
  //rest of implementation
};

suppose the only use of objects of Temp class is to construct objects of Final class, so I wonder if in current standard of c++ , we can using a macro or somehow tell the compiler, since this object of Temp class which I'm defining is only used in one line of code and that is as argument of constructor of Final class; take a name for it yourself so I don't bother myself taking one name for each object Of Temp class?

+3  A: 

Well, that isn't really the only place Temp is used, because you have to construct a Temp object when you call the constructor, right?

As for "taking up names," you can use a nested class:

class Final 
{
public:
    struct ConstructorParameter { char a, b; };

    Final(const ConstructorParameter& in) { ... };
};

Or, of course, you could just make a constructor with the two parameters:

class Final
{
public:
    Final(char a, char b) { ... };
};
James McNellis
+1  A: 

If it's just two sub-objects, use std::pair<char,char>.

If your actual code has more of them, use a tuple (either std::tuple or std::tr1::tuple or boost::tuple.)

sbi
But often named subparts make the code much clearer.
Mark B
suppose we want to use a tuple that has an array with 11 elements, then how is it possible to omit taking name for it ??
Pooria
@Pooria: This all sounds very contrived; if you have eleven objects, then I'd presume those are all related in some way and a named struct would make much, much more sense. Why are you so worried about naming the struct?
James McNellis
take a look at selected answer, you'll get what I meant.
Pooria
A: 

there's no such thing as I want in current c++ standard, but in upcoming c++0x standard, to initialize an object of the Final class, we can write:

Final obj({'a','b'}); 

So no naming for initializer object of the Temp class !!!

Pooria