Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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++ - Fatal编程技术网

C++ 在超类构造函数内初始化结构数组

C++ 在超类构造函数内初始化结构数组,c++,C++,我想调用超类构造函数并直接给它一个“MyStruct”结构数组。我试着在下面演示这一点。在main方法中,我使用structs创建了一个“Foo”实例,但这在“Bar”类中的超类构造函数中不起作用。如何正确实现这一点 struct MyStruct { int x; int y; }; class Foo { private: MyStruct* m_MyStructArray; int m_MyStructLength; public: Foo(MyS

我想调用超类构造函数并直接给它一个“MyStruct”结构数组。我试着在下面演示这一点。在main方法中,我使用structs创建了一个“Foo”实例,但这在“Bar”类中的超类构造函数中不起作用。如何正确实现这一点

struct MyStruct
{
    int x;
    int y;
};

class Foo
{
private:
    MyStruct* m_MyStructArray;
    int m_MyStructLength;
public:
    Foo(MyStruct* myStructArray, int myStructLength)
        : m_MyStructArray(myStructArray), m_MyStructLength(myStructLength) {}
    ~Foo() {}
    const MyStruct* getMyStructArray() { return m_MyStructArray; }
    int getCount() { return m_MyStructLength; }
};

class Bar : Foo
{
public:
    Bar()
        : Foo((MyStruct*) { {10, 10}, { 10, 10 } }, 2) {} //This doesn't work obviously
    ~Bar() {}
};


int main() {
    MyStruct myStructs[] = {
            { 10, 10 },
            { 20, 20 },
    };
    Foo foo(myStructs, 2);
}

通常情况下,问题在于使用过时的语言结构。以下是我们应该如何做到这一点:

#include <vector>
struct MyStruct
{
    int x;
    int y;
};

class Foo
{
private:
    std::vector<MyStruct> m_MyStructArray;
public:

    Foo(const std::vector<MyStruct> myStructArray)
        : m_MyStructArray(std::move(myStructArray)) {}
    ~Foo() = default;
    const auto& getMyStructArray() const { return m_MyStructArray; }
    auto getCount() const { m_MyStructArray.size(); }
};

class Bar : Foo
{
public:
    Bar()
        : Foo({ {10, 10}, { 10, 10 } }) {} //This does work
    ~Bar() = default;
};


int main() {
    Foo foo({{ 10, 10}, {20, 20 }});
}
#包括
结构MyStruct
{
int x;
int-y;
};
福班
{
私人:
std::向量m_MyStructArray;
公众:
Foo(const std::vector myStructArray)
:m_MyStructArray(std::move(MyStructArray)){}
~Foo()=默认值;
const auto&getMyStructArray()const{return m_MyStructArray;}
auto getCount()常量{m_MyStructArray.size();}
};
酒吧类别:富
{
公众:
Bar()
:Foo({10,10},{10,10}}){}//这确实有效
~Bar()=默认值;
};
int main(){
Foo-Foo({10,10},{20,20});
}

通常情况下,问题在于使用过时的语言结构。以下是我们应该如何做到这一点:

#include <vector>
struct MyStruct
{
    int x;
    int y;
};

class Foo
{
private:
    std::vector<MyStruct> m_MyStructArray;
public:

    Foo(const std::vector<MyStruct> myStructArray)
        : m_MyStructArray(std::move(myStructArray)) {}
    ~Foo() = default;
    const auto& getMyStructArray() const { return m_MyStructArray; }
    auto getCount() const { m_MyStructArray.size(); }
};

class Bar : Foo
{
public:
    Bar()
        : Foo({ {10, 10}, { 10, 10 } }) {} //This does work
    ~Bar() = default;
};


int main() {
    Foo foo({{ 10, 10}, {20, 20 }});
}
#包括
结构MyStruct
{
int x;
int-y;
};
福班
{
私人:
std::向量m_MyStructArray;
公众:
Foo(const std::vector myStructArray)
:m_MyStructArray(std::move(MyStructArray)){}
~Foo()=默认值;
const auto&getMyStructArray()const{return m_MyStructArray;}
auto getCount()常量{m_MyStructArray.size();}
};
酒吧类别:富
{
公众:
Bar()
:Foo({10,10},{10,10}}){}//这确实有效
~Bar()=默认值;
};
int main(){
Foo-Foo({10,10},{20,20});
}

我强烈建议远离你的道路。存储指向从调用函数传递的数组的指针有很多缺陷。开始使用
std::vector
(如果大小是动态的)或
std::array
(如果大小是静态的)

话虽如此,如果必须存储指针…

可以使用
Bar
的成员函数返回指向可传递给基类构造函数的数组的指针

class Bar : Foo
{
  public:
    Bar() : Foo(getMyStructArray(), 2) {}
    ~Bar() {}

 private:

   static MyStruct* getMyStructArray()
   {
     static MyStruct myStructs[] = { { 10, 10 }, { 10, 10 }};
     return myStructs;
   } 
};

我强烈建议远离你的道路。存储指向从调用函数传递的数组的指针有很多缺陷。开始使用
std::vector
(如果大小是动态的)或
std::array
(如果大小是静态的)

话虽如此,如果必须存储指针…

可以使用
Bar
的成员函数返回指向可传递给基类构造函数的数组的指针

class Bar : Foo
{
  public:
    Bar() : Foo(getMyStructArray(), 2) {}
    ~Bar() {}

 private:

   static MyStruct* getMyStructArray()
   {
     static MyStruct myStructs[] = { { 10, 10 }, { 10, 10 }};
     return myStructs;
   } 
};

您可能需要创建一个静态数组并将指针转发给<代码> fo。请考虑<代码> MyMyStRealTray< /Cux>是指针,而不是数组。@ VU1P3N0X HMM…是的,我也考虑过,但它不是很优雅。如果没有其他解决方案,我会这样做。
Foo
使用
MyStruct*
int
参数。在Bar中,您并没有将这2个信息提供给构造函数。不要使用这样的静态数组。使用<代码>向量或<代码> STD::数组,PrErelm将消失。您可能需要创建一个静态数组,并将其指向一个指针,以<代码> Foo。考虑 MyMyStRealTray< /Co>是指针,而不是数组。@ Vu1P3N0X HMM…是的,我也考虑过,但它不是很优雅。如果没有其他解决方案,我会这样做。
Foo
使用
MyStruct*
int
参数。在Bar中,您并没有将这2个信息提供给构造函数。不要使用这样的静态数组。使用
vector
std::array
你的问题就会消失。我觉得这在很大程度上是我答案的副本?@SergeyA,你是对的。在这种情况下,我们的想法是一样的。我觉得这在很大程度上是我答案的副本?@SergeyA,你是对的。在这个特殊情况下,我们的想法是一样的。