小编use*_*384的帖子

在C++中将结构作为构造函数参数传递

我看起来尽我所能,在这个特定主题上找不到多少.我必须获取大量变量,可能是多个对象并通过一组函数传递它们,所以我认为这是最好的方法.

我想将结构传递给未定义结构的类的构造函数.这可能吗?我的代码看起来像这样:

class myClass
{
protected:
   double someVar;
   .
   .
public:
   myClass(struct structName);  //compiler is ok with this
   myClass();                   //default construct
};

struct structName
{
   double someOtherVar;
   .
   .
};

myClass::myClass(??????)  ///I don't know what goes here
{ 
  someVar = structName.someOtherVar; ///this is what I want to do
} 
Run Code Online (Sandbox Code Playgroud)

编辑:我应该在类中声明一个结构并使用该结构作为参数吗?

c++ struct class

2
推荐指数
1
解决办法
3382
查看次数

为什么这个指数有效?C++

所有,我的教授给了我们以下循环用于本周的任务:

char *ptr1, *ptr2;  
char c;
ptr1 = &arr[0];  
ptr2 = &arr[idx-1];

while(ptr1 < ptr2){
  c     = *ptr1;  
  *ptr1 = *ptr2;
  *ptr2 = c;

  ptr1++;
  ptr2--;
}  
Run Code Online (Sandbox Code Playgroud)

这是关于ptr1与ptr2的位置吗?程序反转c_str并且工作正常,我只是不明白为什么.

c++ while-loop

0
推荐指数
1
解决办法
122
查看次数

标签 统计

c++ ×2

class ×1

struct ×1

while-loop ×1