在C+中调用构造函数+; 我刚开始用C++。我有一个类ourVector,在这个类中我还有一个名为ourVector()的构造函数,它包括变量的初始化。我需要在main中的每个循环中重置这些变量,但我不知道如何在main中调用构造函数 class ourVector ourVector() { vectorCap = 20; vectorSize = 0; }

在C+中调用构造函数+; 我刚开始用C++。我有一个类ourVector,在这个类中我还有一个名为ourVector()的构造函数,它包括变量的初始化。我需要在main中的每个循环中重置这些变量,但我不知道如何在main中调用构造函数 class ourVector ourVector() { vectorCap = 20; vectorSize = 0; },c++,vector,constructor,C++,Vector,Constructor,我的类对象主要称为向量测试 我只是想弄清楚如何调用ourVector(),而不会出现错误,这样我就可以将它放在main循环的末尾,以清除并重新初始化变量 任何帮助都将不胜感激 谢谢大家! 构造函数仅在实例化对象时调用;不能显式调用它来重新初始化变量。但是您可以从构造函数中调用公共成员setter函数。然后可以从循环中再次调用setter函数 class ourVector { int vectorCap ; int const defaultVecto

我的类对象主要称为向量测试

我只是想弄清楚如何调用ourVector(),而不会出现错误,这样我就可以将它放在main循环的末尾,以清除并重新初始化变量

任何帮助都将不胜感激


谢谢大家!

构造函数仅在实例化对象时调用;不能显式调用它来重新初始化变量。但是您可以从构造函数中调用公共成员setter函数。然后可以从循环中再次调用setter函数

class ourVector
{  int              vectorCap      ;
   int const defaultVectorCap  = 20; // No magic numbers!
   int              vectorSize     ;
   int const defaultVectorSize =  0;
   int       roomToGrow            ; // To illustrate a point about setters.
public:
   // Constructors
   ourVector() // Use this constructor if you don't know the proper initial values
               // (you probably always do or never do). 
   {  setVectorCap (defaultVectorCap );
      setVectorSize(defaultVectorSize);
   } // End of default constructor

   ourVector(   int vecCap, int vecSize)  // Lining stuff up improves readability
   {  setVectorCap (vecCap             ); // (e.g. understanding the code and
      setVectorSize(           vecSize ); // spotting errors easily).
                                          // It has helped me spot and avoid
                                          // bugs and saved me many many hours.
                                          // Horizontal white space is cheap!
                                          // --Not so forvertical white space IMHO.

   // Setters
   void setVectorCap(int vecCap)
   {  vectorCap        = vecCap;
      // I might need this internally in the class.
      // Setters can do more than just set a single value.
      roomToGrow = vectorCap - vectorSize;
   } // End of setVector w/ parameter

   void setVectorSize(int vecSize)
   {  vectorSize        = vecSize;
      roomToGrow = vectorCap - vectorSize; // Ok, redundant code.  But I did say
                                           // "As much as practical...."
   } // End of setVectorCap w/ parameter

   void setVectorSize()  // Set to default
   {  // Don't just set it here--leads to poor maintainability, i.e. future bugs.
      // As much as practical, redundant code should be avoided.
      // Call the setter that takes the parameter instead.
      setVectorSize(defaultVectorSize);
   } // End of setVectorSize for default size

   void setVectorCap()  // Set to default
   {  setVectorCap (defaultVectorCap );
   } // End of setVectorCap for default size

}; // End of class ourVector
#include <cstdio>
#include <cstdlib>
#include "ourVector.hpp"

int main(int argc, char *argv[]);
void doSomething(ourVector oV  );

int main(int argc, char *argv[])
{  ourVector oV;
   // Or, if you want non-default values,
 //int mySize =  2;
 //int myCap  = 30;
 //ourVector(mySize, myCap);

   for (int i = 0; i<10; i++)
   {  // Set fields to original size.
      oV.setVectorSize();
      oV.setVectorCap ();
      // Or
      //oV.setVectorSize(mySize);
      //oV.setVectorCap (myCap );

      // Whatever it is your are doing
      // ...
  }

   // Do whatever with your vector.  If you don't need it anymore, you probably should
   // have instantiated it inside the for() block (unless the constructor is
   // computationally expensive).
   doSomething(oV);
   return 0;
} // End of main()

void doSomething(ourVector oV) {}
类向量
{int向量帽;
int const defaultVectorCap=20;//没有幻数!
int向量大小;
int const defaultVectorSize=0;
int roomToGrow;//说明关于setter的一点。
公众:
//建设者
ourVector()//如果不知道正确的初始值,请使用此构造函数
//(你可能总是这样做或从不这样做)。
{setVectorCap(defaultVectorCap);
设置向量大小(默认向量大小);
}//默认构造函数的结尾
ourVector(int-vecCap,int-vecSize)//将内容排列起来可以提高可读性
{setVectorCap(vecCap);//(例如,理解代码和
setVectorSize(vecSize);//轻松发现错误)。
//它帮助我发现并避免
//这帮我节省了很多时间。
//水平空白是便宜的!
//--垂直空白IMHO不是这样。
//二传手
void setVectorCap(int vecCap)
{vectorCap=vecCap;
//我可能在课堂上需要这个。
//setter可以做的不仅仅是设置一个值。
roomToGrow=向量上限-向量大小;
}//setVector w/参数的结尾
void setVectorSize(int vecSize)
{vectorSize=vecSize;
roomToGrow=vectorCap-vectorSize;//好的,冗余代码。但我确实说过
//“尽可能实用……”
}//setVectorCap的结尾,带参数
void setVectorSize()//设置为默认值
{//不要只在这里设置它——这会导致较差的可维护性,即将来的bug。
//尽可能避免使用冗余代码。
//调用接受该参数的setter。
设置向量大小(默认向量大小);
}//默认大小的setVectorSize结束
void setVectorCap()//设置为默认值
{setVectorCap(defaultVectorCap);
}//默认大小的setVectorCap结束
};//课程结束
#包括
#包括
#包括“ourVector.hpp”
intmain(intargc,char*argv[]);
无效剂量测定(Ourv);
int main(int argc,char*argv[])
{Ourov;
//或者,如果需要非默认值,
//int mySize=2;
//int myCap=30;
//我们的载体(mySize,myCap);

对于(int i=0;i),只有在实例化对象时才会调用构造函数;您不能显式调用它来重新初始化变量。但您可以从构造函数调用公共成员setter函数。然后,您可以从循环中再次调用setter函数

class ourVector
{  int              vectorCap      ;
   int const defaultVectorCap  = 20; // No magic numbers!
   int              vectorSize     ;
   int const defaultVectorSize =  0;
   int       roomToGrow            ; // To illustrate a point about setters.
public:
   // Constructors
   ourVector() // Use this constructor if you don't know the proper initial values
               // (you probably always do or never do). 
   {  setVectorCap (defaultVectorCap );
      setVectorSize(defaultVectorSize);
   } // End of default constructor

   ourVector(   int vecCap, int vecSize)  // Lining stuff up improves readability
   {  setVectorCap (vecCap             ); // (e.g. understanding the code and
      setVectorSize(           vecSize ); // spotting errors easily).
                                          // It has helped me spot and avoid
                                          // bugs and saved me many many hours.
                                          // Horizontal white space is cheap!
                                          // --Not so forvertical white space IMHO.

   // Setters
   void setVectorCap(int vecCap)
   {  vectorCap        = vecCap;
      // I might need this internally in the class.
      // Setters can do more than just set a single value.
      roomToGrow = vectorCap - vectorSize;
   } // End of setVector w/ parameter

   void setVectorSize(int vecSize)
   {  vectorSize        = vecSize;
      roomToGrow = vectorCap - vectorSize; // Ok, redundant code.  But I did say
                                           // "As much as practical...."
   } // End of setVectorCap w/ parameter

   void setVectorSize()  // Set to default
   {  // Don't just set it here--leads to poor maintainability, i.e. future bugs.
      // As much as practical, redundant code should be avoided.
      // Call the setter that takes the parameter instead.
      setVectorSize(defaultVectorSize);
   } // End of setVectorSize for default size

   void setVectorCap()  // Set to default
   {  setVectorCap (defaultVectorCap );
   } // End of setVectorCap for default size

}; // End of class ourVector
#include <cstdio>
#include <cstdlib>
#include "ourVector.hpp"

int main(int argc, char *argv[]);
void doSomething(ourVector oV  );

int main(int argc, char *argv[])
{  ourVector oV;
   // Or, if you want non-default values,
 //int mySize =  2;
 //int myCap  = 30;
 //ourVector(mySize, myCap);

   for (int i = 0; i<10; i++)
   {  // Set fields to original size.
      oV.setVectorSize();
      oV.setVectorCap ();
      // Or
      //oV.setVectorSize(mySize);
      //oV.setVectorCap (myCap );

      // Whatever it is your are doing
      // ...
  }

   // Do whatever with your vector.  If you don't need it anymore, you probably should
   // have instantiated it inside the for() block (unless the constructor is
   // computationally expensive).
   doSomething(oV);
   return 0;
} // End of main()

void doSomething(ourVector oV) {}
类向量
{int向量帽;
int const defaultVectorCap=20;//没有幻数!
int向量大小;
int const defaultVectorSize=0;
int roomToGrow;//说明关于setter的一点。
公众:
//建设者
ourVector()//如果不知道正确的初始值,请使用此构造函数
//(你可能总是这样做或从不这样做)。
{setVectorCap(defaultVectorCap);
设置向量大小(默认向量大小);
}//默认构造函数的结尾
ourVector(int-vecCap,int-vecSize)//将内容排列起来可以提高可读性
{setVectorCap(vecCap);//(例如,理解代码和
setVectorSize(vecSize);//轻松发现错误)。
//它帮助我发现并避免
//这帮我节省了很多时间。
//水平空白是便宜的!
//--垂直空白IMHO不是这样。
//二传手
void setVectorCap(int vecCap)
{vectorCap=vecCap;
//我可能在课堂上需要这个。
//setter可以做的不仅仅是设置一个值。
roomToGrow=向量上限-向量大小;
}//setVector w/参数的结尾
void setVectorSize(int vecSize)
{vectorSize=vecSize;
roomToGrow=vectorCap-vectorSize;//好的,冗余代码。但我确实说过
//“尽可能实用……”
}//setVectorCap的结尾,带参数
void setVectorSize()//设置为默认值
{//不要只在这里设置它——这会导致较差的可维护性,即将来的bug。
//尽可能避免使用冗余代码。
//调用接受该参数的setter。
设置向量大小(默认向量大小);
}//默认大小的setVectorSize结束
void setVectorCap()//设置为默认值
{setVectorCap(defaultVectorCap);
}//默认大小的setVectorCap结束
};//课程结束
#包括
#包括
#包括“ourVector.hpp”
intmain(intargc,char*argv[]);
无效剂量测定(Ourv);
int main(int argc,char*argv[])
{Ourov;
//或者,如果需要非默认值,
//int mySize=2;
//int myCap=30;
//o
ourVector()
{
    setVCap(20);
    setVSize(0);
}

ourVector(int c, int s)
{
    setVCap(c);
    setVSize(s);
}
int main () {
    ...
    while (...) { // <- this represents your loop

        ourVector v; // <- each time through, 'v' is constructed

        ... // <- do whatever with 'v' here

    } // <- that 'v' goes away when it goes out of scope
    ...
}