将值传递给构造函数。 C ++

时间:2013-10-31 07:31:38

标签: c++

通常这是将变量传递给其他类的构造函数的方法。

SampleA.cpp

#include SampleA.h
#include <iostream>
using namespace std;

SampleA::SampleA(string text) 
{
    setText(text);
}    

string SampleA::getText() {
    return text; 
}

void SampleA::setText(string text) {
    this->text = text;
}

void SampleA::displayText() {
    string displayText;
    displayText = getText();
    cout << displayText << endl;
}

SampleA.h

#include <iostream>
#ifndef _testing_ SampleA_h
#define _testing_ SampleA_h

class SampleA     {

private:
    std::string text;
public :    
    SampleA() 
    {
        text = ""; 
    };//default constructor

    SampleA(std::string);
    std::string getText();
    void setText(std::string text);   
}; 

main.cpp

  #include <iostream>
  #include SampleA.h
  using namespace std;
  SampleA outputTextValue;

  int main ()  {
     string input;
     cout << "Enter a text" << endl;
     cin >> input;
     //pass the value using SampleA class consturctor
     SampleA storeText(input);
     //output the text from displayText() method from SampleA class
     outputTextValue.displayText(); 
  }

我想知道你是否可以为二维阵列做同样的事情。

或者更确切地说,如果您想将2d数组值传递给其他类的consturctor,我应该使用什么方法? (*下面显示的代码是错误的,因为它只是一个粗略的例子)

SampleA.cpp

#include SampleA.h
#include <iostream>
using namespace std;

SampleA::SampleA(int 2Darray[][2]) 
{
    setText(2Darray[][2]);
}    

string SampleA::get2DArray() {
    return 2Darray[][2]; 
}

void SampleA::set2DArray(string int 2Darray[][2]) {
    this->2Darray[][2] = 2Darray[][2];
}


 void SampleA::displayNumber() {
    cout << get2DArray(); << endl;
}

SampleA.h

#include <iostream>
#ifndef _testing_ SampleA_h
#define _testing_ SampleA_h

class SampleA     {

private:
    int 2Darray[][2];
public :    
    SampleA() 
    {
        2Darray[][2]; 
    };//default constructor

    SampleA(2Darray[][2]);
    std::string get2DArray();
    void set2DArray(2Darray[][2]);   
}; 

main.cpp

  #include <iostream>
  #include SampleA.h
  using namespace std;

  SampleA outputMethod;
  int main ()  {
     int storeValue [2][2];
     for(int i =0; i<2; i++) {
        cout << "Enter first number" << endl;
        cin >> storeValue[i][0];
        cout << "Enter first number" << endl;
        cin >> storeValue[i][1];
   }

     //pass the 2dArray value using SampleA class consturctor
     SampleA storeText(storeValue);
     // output method displayNumber() method from SampleA class
     outputMethod.displayNumber();
  }

2 个答案:

答案 0 :(得分:2)

是的,但不是一个正确的矩阵,因为当作为参数传递时,数组会衰减为指针。

你必须这样做。

class SampleA
{
    int (*some2darray)[2];  // `some2darray` is a pointer to an array of two integers
    size_t size;            // Size of "outer" array

public:
    SampleA(int (*arg)[2], size_t sz)
        : some2darray(arg), size(sz)
    {}

    void displayText() const
    {
        std::cout << "Values = { ";

        for (size_t i = 0; i < size; ++i)
            std::cout << "{ " << some2darray[i][0] << ", " some2darray[i][1] << " } ";

        std::cout << "}\n";
    }
};

然后做例如。

int storeValue[2][2];
...
SampleA store(storeValue, 2);
store.displayText();

答案 1 :(得分:0)

传递2D数组:

method(array_name);

没有括号没有尺寸指示。

setText(2Darray);