Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可以用C+给出类的定义吗+;在分配过程中,java允许这样做_C++_Anonymous Class - Fatal编程技术网

C++ 可以用C+给出类的定义吗+;在分配过程中,java允许这样做

C++ 可以用C+给出类的定义吗+;在分配过程中,java允许这样做,c++,anonymous-class,C++,Anonymous Class,或者简单地说 我能做点像这样的事吗 class A { public: virtual void foo() = 0; }; class B { public: A *a; b(){ a = new A() { void foo() {printf("hello");} } }; 不,C++没有匿名类,如java的< /P> 您可以定义本地类,如下所示: class B { public: A *a; b(){

或者简单地说

我能做点像这样的事吗

class A {
public:
  virtual void foo() = 0;
};

class B {
  public:
    A *a;
    b(){
       a = new A() { void foo() {printf("hello");}
    }
};

<>不,C++没有匿名类,如java的< /P> 您可以定义本地类,如下所示:

class B {
  public:
    A *a;
    b(){
       struct my_little_class : public A {
           void foo() {printf("hello");}
       };
       a = new my_little_class();
    }
};
或者可能只是一个嵌套类:

class B {
  private:
    struct my_little_class : public A {
        void foo() {printf("hello");}
    };

  public:
    A *a;

    b(){
       a = new my_little_class();
    }
};
在C++03中,局部类有一些在C++11中取消的限制(例如,它们不能用作模板参数)


在Java中,匿名类有时用于执行其他语言对匿名函数所做的操作,例如,当您创建
Runnable
的匿名实现时。C++ 11有匿名函数(也称为lambdas),所以如果你想实现的话,它可以是一个选项。

< P>不。C++中的所有东西都必须在它被使用之前定义。在您的情况下,由于要重写
A::foo()
,因此必须派生一个新类,然后
B
可以实例化该类,例如:

class A
{ 
public: 
  virtual void foo() = 0; 
}; 

class A1 : public A
{
public:
  void foo() { printf("hello"); }
}; 

class B
{ 
public: 
  A *a; 
  B()
  { 
    a = new A1(); 
  } 
};
没有

但在Java中将回调传递给API时,这种习惯用法很常见。如果这是您想要的(为API注册回调),那么您可能希望使用信号,比如在或上实现(在这种情况下最好的方式)

也可以,在语法上,你想要的更接近,新的C++规范(C++ 11,最近编译程序支持)也允许lambda函数:

template<class F>
void func(F callable) {
  callable(); // Callable is an object that can be called as a function.
}

void a() {
  method([]() {
    printf("hello");
  }); // This anonymous function was defined here...
}
该类甚至可以是匿名的,但您只能在堆栈上创建对象(即,您不能在匿名类上使用
new
,它将在
函数
返回时解除分配):


与其他人的答案相同,但如果您希望模仿这种行为,您可以(但我不推荐):

struct接口
{
虚空doStuff()常量=0;
虚拟~Interface(){}
};
#定义新名称(tmp_名称、父项、正文、参数…)\
({                                                                    \
类tmp_名称:\
父母\
{                                                                   \
身体\
};                                                                  \
新tmp#U名称(##args)\
})
接口*getDefault()
{
返回newAnon(_tmp__;,公共接口,
公众:
虚空doStuff()常量{

如果我要实现这样的事情,最好的策略是什么。@ USE210504:邓诺,我没有执行<代码> Foo< /Calp>的经验。换句话说,我不知道你在执行什么。C++确实有匿名类。@ IVELLA:嗯。你说得对。它没有java中的匿名类。我把它固定了。另一方面,也有lambda函数,它们确实可以在运行中创建闭包。你的
类A
应该有一个虚拟析构函数才能正确删除:
virtual~A(){}<你不知道java中的匿名类是什么,或者你不明白为什么有人会试图把这个成语转换成C++?难道B-()不应该是B()吗?你可以使用<代码> A=新的DeCype(OBJ)(;)/<代码>匿名类,虽然我不明白为什么有人想要这个。
class A {
public:
  virtual void foo() = 0;
};

class B {
  public:
    A *a;
    void b(){
       class MyImplementation: public A { 
         public: void foo() { printf("hello"); }
       };
       a = new MyImplementation();
    }
};
void function_that_uses_but_does_not_stores_A(A* obj);

void function(){
    class : public A { 
         public: void foo() { printf("hello"); }
    } obj;

    function_that_uses_but_does_not_stores_A(&obj);
 };
    struct          Interface
    {
      virtual void  doStuff() const = 0;
      virtual       ~Interface() {}
    };

    #define newAnon(tmp_name, parents, body, args...)                       \
      ({                                                                    \
        class              tmp_name :                                      \
          parents                                                           \
        {                                                                   \
          body;                                                             \
        };                                                                  \
        new tmp_name(##args);                                               \
      })

    Interface       *getDefault()
    {
      return newAnon(__tmp__, public Interface,
public:
                     virtual void doStuff() const {
                       std::cout << "Some would say i'm the reverse" << std::endl;
                     });
    }
    struct          Interface
    {
      virtual void  doStuff() const = 0;
      virtual void  undoStuff() const = 0;
      virtual       ~Interface() {}
    };

    newAnon(__tmp__, Interface,
                     static int &i() {
                       static int i(0);
                       return i;
                     }

public:    
                     virtual void doStuff() const {
                       std::cout << "call n°" << i()++ << std::endl;
                     }

                     virtual void undoStuff() const {
                       std::cout << "uncall n°" << i()-- << std::endl;
                     });
  auto tmp = newAnon(...);

  struct Try : decltype(*tmp+) {
    Try() { std::cout << "Lol" << std::endl; }
  };

  Try a; // => will print "Lol"
#define newAnon(code...) \
     [&](auto&&... args) {\
          struct __this__ : code;\
          return std::make_unique<__this__>(std::forward<decltype(args)>(args)...); \
      }

auto ptr = new_anon(interface { ... })(arguments);