在接口类和c ++中的派生类中共享默认参数值

时间:2016-12-02 08:50:33

标签: c++

我想在接口类和Derived类中使用float值(defaultColour)作为默认参数。那么我应该如何定义浮动defaultColour []。

//Interface class
static float defaultColour[]={0,0,0,0}; //This become error

class Interface{
    virtual void print_color(float *color = defaultColour)=0;
}

//Use Interface  Derived.h 
class Derived : public Interface{
    void print_color(float *a = defaultColour);
}

//Derived.cpp 
void Derived :: print_color(float *a){
    //some code using color a[]
}

3 个答案:

答案 0 :(得分:2)

默认数组值实际上不应该是个问题。如果您的示例中的语法错误已整理出来,请参阅以下示例works fine for me

#include <iostream>
#include <memory>

static float default_array[] = { 42.0f, 13.0f, 2.0f, 0.0f };

struct Interface
{
  virtual void foo(float *a = default_array) = 0;
};

struct Derived : public Interface
{
  void foo(float *a);
};

// Silly contrived example: output array values until a 0 is hit.  You
// better make sure there actually is a 0 at the end.
void Derived::foo(float *a)
{
  while (*a != 0.0f)
  {
    std::cout << *a << std::endl;
    ++a;
  }
}

int main()
{
  std::unique_ptr<Interface> i(new Derived);
  std::cout << "i->foo():" << std::endl;
  i->foo();
  float other_array[] = { 1.0f, -2.0f, 3.0f, -4.0f, 0.0f };
  std::cout << "i->foo(other_array):" << std::endl;
  i->foo(other_array);
}

但请注意,如果多个编译单元包含该标头,则在标头中定义default_array将违反单一定义规则。为避免这种情况,您必须使用@ MartinBonner的解决方案并使该数组成为静态成员变量。

如果您知道只能通过指向foo() 的指针调用Interface ,则无需在{{1}中重新指定默认值宣言。但是,如果你也希望做这样的事情:

Derived::foo

然后你会遇到问题,因为 Derived der; der.foo(); 实际上并不存在。 GCC会抱怨:

Derived::foo()

在这种情况下,一些通用的解决方法可能是将以下内联函数声明添加到test.cpp: In function ‘int main()’: test.cpp:37:15: error: no matching function for call to ‘Derived::foo()’ der.foo(); ^ test.cpp:37:15: note: candidate is: test.cpp:18:10: note: virtual void Derived::foo(float*) void Derived::foo(float *a) ^ test.cpp:18:10: note: candidate expects 1 argument, 0 provided

Derived

答案 1 :(得分:1)

使defaultColour成为Interface的const静态公共成员。

Interface.h

    class Interface{
    public:
        static float defaultColour[colourSize]; // You have got a const for the
                                                // array size somewhere,
                                                // haven't you?

        virtual void print_color(float *color = defaultColour)=0;

    }; // Need trailing ; here

Derived.h

    class Derived : public Interface {  // Need to derive from Interface.
    public:
        void print_color(float *a = defaultColour); // Just declare function here.
    };

Derived.cpp

    void Derived::print_color(float *a){  // define function here.
            //some code using color a[]
    }

Interface.cpp(这可能是一个新文件)

    float Interface::defaultColour[] = {255,255,255,150};

答案 2 :(得分:0)

好的。感谢每一个人。我用这种方式解决了。有什么不好的规则吗?

//Interface class
    class Interface{
        public:
        virtual void print_color(const float *color = defaultColour)=0;
        protected:
        static const float defaultColour[4];
    }

    //Use Interface  Derived.h 
    class Derived : public Interface{
        void print_color(const float *a = defaultColour);
    }

    //Derived.cpp 
    Interface::defaultColour[]={0,0,0,0};
    void Derived :: print_color(const float *a){
            //some code using color a[]
    }