tags:

views:

398

answers:

4

How do I pass this instance as a parameter into a function?

class
{
    public:
    void foo();
} bar;

Do I have to name the class?
It is copyable since I haven't made the class's copy ctor private.
So how is it possible if at all?

A: 

Yes you have to name the class to pass it a instance of to the function. As you have not provided your own copy ctor compiler will generate its own and use it.

Naveen
A: 

Hmmm, nameless class.

You could pass a pointer to it as void *.

Since you cannot refer to it by name, you cannot call its constructor, copy constructor, or destructor except for the one instance, so you cannot copy it.

EDIT: you could still pass it to a template function which could copy it.

REEDIT: edit was wrong.

Joshua
No, it cannot. 14.3.1 Template type arguments: '2 A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter'
David Rodríguez - dribeas
+2  A: 

Why should you create an anonymous class when you want to pass it to a function?

Just explicitly declare the class:

class Foo {
  // ...
};

void Method(Foo instance); 

int main() {
    Foo bar;
    Method(bar);
}

The 2nd possibility would be using a template-function, so the compiler would infer the type (Note that this is not standard-compatible!)

#include <iostream>

using namespace std;

template <typename T>
void SayFoo(T& arg) {
    arg.Foo();
}

int main() {

    class {
    public: 
     void Foo() { cout << "Hi" << endl; }
    } Bar;

    Bar.Foo();

    SayFoo(Bar);

    return 0;
}

There is no problem with copying the class since the compiler generated the copy constructor automatically and you can use tools like boost::typeof in order to avoid referring to the type explicitly.

BOOST::AUTO(copy, Bar);

Another approch is using (relatively slow) runtime-polymorphism (interfaces/inheritance).

Dario
could you show me an example of using boost::typeof?BOOST::AUTO doesn't say much to me.Is it still necessary with the new standard?
the_drow
Option 2 is not an option. From the standard 14.3.1 Template type arguments: '2 A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter'
David Rodríguez - dribeas
@the_drow:No, C++0x will introduce the auto-keyword which will infer the type automatically.You'll have to install boost in order to use boost::typeof
Dario
+3  A: 

Maybe it would be better if you explicit what you want to do. Why do you want to create an unnamed class? Does it conform to an interface? Unnamed classes are quite limited, they cannot be used as parameters to functions, they cannot be used as template type-parameters...

Now if you are implmenting an interface then you can pass references to that interface:

class interface {
public:
   virtual void f() const = 0;
};
void function( interface const& o )
{
   o.f();
}
int main()
{
   class : public interface {
   public:
      virtual void f() const {
         std::cout << "bar" << std::endl;
      }
   } bar;
   function( bar ); // will printout "bar"
}

NOTE: For all those answers that consider template arguments as an option, unnamed classes cannot be passed as template type arguments.

C++ Standard. 14.3.1, paragraph 2:

2 A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

If you test with comeau compiler (the link is for the online tryout) you will get the following error:

error: a template argument may not reference an unnamed type

As a side note, comeau compiler is the most standard compliant compiler I know of, besides being the one with the most helpful error diagnostics I have tried.

NOTE: Comeau and gcc (g++ 4.0) give an error with the code above. Intel compiler (and from other peoples comments MSVS 2008) accept using unnamed classes as template parameters, against the standard.

David Rodríguez - dribeas
I'm asking because I'm exploring some of the C++ features that I almost never seen in use.So what is the difference between an unnamed class and a singleton?
the_drow
They have nothing in common. The singleton pattern deals with the unicity of the element (a single object, accessible from anywhere). You can implement a singleton (as everyone else does) with named classes.
David Rodríguez - dribeas
FYI: This unnecessary restriction on local classes and unnamed types as template parameters has been lifted from C++0x :)
Faisal Vali