#在 class 外定义宏

#define macro outside class

我不太明白这里宏的用法:

在Dog.cpp中,在class范围之外有这个:

BEGIN_SETPARAM_MAP(Dog)
    MAP_SETPARAM(eParam_Macro_Eat,      Eat)
    MAP_SETPARAM(eParam_Macro_Walk,     Walk)
    MAP_SETPARAM(eParam_Macro_Sleep,    Sleep)
END_GETPARAM_MAP(Dog)

并且在 Animals.h 中有一个宏定义,也在 Animal class 范围之外找到:


class Animal
{
protected:
   HRESULT  MapSetParamHandler(ULONG paramId, ParamValueHandler handler);
   virtual void SetupSetParamMap(void) {}
};

#define BEGIN_SETPARAM_MAP(className)   void className::SetupSetParamMap(void) {typedef className ThisClass;
#define MAP_SETPARAM(paramId, handler)  MapSetParamHandler(paramId, static_cast<ParamValueHandler>(&ThisClass::handler));
#define END_SETPARAM_MAP(className)     }

我不明白这到底是怎么回事。为什么允许在class之外重新定义方法SetupSetParamMap()?如果是这样,这会覆盖 Animal class 上的实现吗?

我不知道上面的代码如何编译得很好。

我尝试创建一个更简单的示例,但出现语法错误:

// Example program
#include <iostream>
#include <string>

class Animal
{
    public:
    void cuddle()
    {
        std::cout << "Cuddle" << std::endl;
    }
};

class Dog : public Animal
{
    public:
    void bark()
    {
        std::cout << "Bark" << std::endl;
        cuddle();
    }
    virtual void sleep(){}
};

#define BEGIN_SETPARAM_MAP(className) void Dog::sleep() \
{ \
    std::cout << "sleep" << std::endl; \
} 

BEGIN_SETPARAM_MAP(Dog);

int main()
{
    Dog dog;
    dog.bark();
    dog.sleep();

    return 0;
}

这里是错误:

25:44: error: redefinition of 'void Dog::sleep()'
30:1: note: in expansion of macro 'BEGIN_SETPARAM_MAP'
22:18: note: 'virtual void Dog::sleep()' previously defined here

有人帮忙吗??

I don't understand how this actually works. Why is it allowed to re-define the method SetupSetParamMap() outside the class?

该代码之所以有效,是因为它没有定义方法 Animal::SetupSetParamMap。该宏的作用是允许您为另一种对象类型实现 SomeDerivedAnimalType::SetupSetParamMap。如果您尝试执行:BEGIN_SETPARAM_MAP(Animal),那么现在您将遇到 Animal::SetupSetParamMap 的重定义错误。但是 BEGIN_SETPARAM_MAP(Sheep) 不会造成问题。

关于第二个代码示例,编译器错误是不言自明的...(您显然已经识别出来了!)

class Dog : public Animal
{
    public:
    void bark()
    {
        std::cout << "Bark" << std::endl;
        cuddle();
    }
    virtual void sleep(){}  ///< I AM THE IMPLEMENTATION OF Dog::sleep
};

/// I AM ALSO THE IMPLEMENTATION OF Dog::sleep, and therefore an error... 
#define BEGIN_SETPARAM_MAP(className) void Dog::sleep() \
{ \
    std::cout << "sleep" << std::endl; \
} 

这里有一个稍微简单的例子,可以让您理解 ;)

class Animal
{
    public:

    /// I need to be implemented for each animal
    /// It's boiler-plate repetition, so lets macro this
    virtual void speak() = 0;
};

// use within the class definition to add capabilities
#define CAN_SPEAK(animalType) void speak() override;

// use to implement the boiler plate code of the method
#define MAKE_SPEAK(animalType, noise) void animalType::speak() \
{ \
    std::cout << noise << std::endl; \
} 

// here's a dog that says woof
class Dog : public Animal
{
    public:
    CAN_SPEAK(Dog)
};
MAKE_SPEAK(Dog, "woof")

// ... and a sheep that says baa
class Sheep : public Animal
{
    public:
    CAN_SPEAK(Sheep)
};
MAKE_SPEAK(Sheep, "baa")

int main()
{
  std::vector<Animal*> animals;
  animals.push_back(new Dog);
  animals.push_back(new Sheep); 
  for(auto animal : animals)
  {
    animal->speak();
  }

  /* yes, I should be freeing the memory here... */

  return 0;
}