0

我有一个这样的课程:-

class Con
{
 public:
  struct MachineList
  {
    BSTR AccountId;
    BSTR MachineId;
    BSTR Make;
    char* Make1;
    BSTR Model;
    char* Model1;
    BSTR SerialNumber;
    BSTR IpAddress;
    char* IpAddress1;
    BSTR Port;
    int Port1;
    BSTR LocationCode;
    } machinelist[100] ;
   int w;
 } ;

我创建了一个Con类对象Con m_con;

我还有一堂课Test

class Test
{
 public:
  void fun();//i want to pass the object of the structure that i created in Con
  //what arguments should i pass in fun function?
};
4

3 回答 3

4

Do you want to pass the whole machinelist array?

void fun(const Con::MachineList (&argument)[100] );

But easier would be to typedef it:

class Con { 
 public:
    struct MachineList{ ... };
    typedef MachineList (Machines)[100];

    Machines machines;
};

and use

void fun(const Con::Machines& m);

To be called like:

Con c;
fun( c.machines );
于 2012-10-26T18:58:38.260 回答
2
class Con {
public:
   struct Machine {
      BSTR  AccountId;
      BSTR  MachineId;
      BSTR  Make;
      char* Make1;
      BSTR  Model;
      char* Model1;
      BSTR  SerialNumber;
      BSTR  IpAddress;
      char* IpAddress1;
      BSTR  Port;
      int   Port1;
      BSTR  LocationCode;
   };
   typedef Machine Machines[100];

   Machines machines;
   int      w;
};

/**
 * passing a reference to a single machine, which fun might change
 */
void fun0( Con::Machine & machine );

/**
 * passing a reference to a single machine, which fun cannot change
 */
void fun1( const Con::Machine & machine );

/**
 * passing a reference to the entire array of machines, which fun may change
 */
void fun2( Con::Machines & machines );

/**
 * passing a reference to the entire array of machines, which fun cannot change
 */
void fun3( const Con::Machines & machines );

我不明白你想要哪个版本的乐趣,所以我做了四个......

于 2012-10-26T19:02:53.697 回答
0
class Test
{
public:
void fun(Con::MachineList);
};

Test t;
Con c;
t.fun(c.machinelist[0]);

目前 Con::MachinList 类型看起来不适合复制,因此您应该定义适当的构造函数和赋值运算符,或者您应该通过声明funasvoid fun(Con::MachineList const &);而不是我上面显示的那样通过引用传递。

MachinList 也可能需要一个适当的析构函数......

于 2012-10-26T18:55:57.607 回答