[英]Calling a constructor in C++
我只是从C ++开始。 我有一个类ourVector,在该类中,我也有一个名为ourVector()的构造函数,它包含变量的初始化。 我需要这些变量在main中的每个循环中重置,但是我不知道如何在main中调用构造函数。
class ourVector
ourVector()
{
vectorCap = 20;
vectorSize = 0;
}
总的来说,我的类对象称为ourVector vectorTest;
我只是想弄清楚如何在不出错的情况下调用ourVector(),因此可以将其放在main循环的末尾,以清除并重新初始化变量。
任何帮助将不胜感激。
谢谢!
通常,当您发现自己在做这样的事情时,这表明可能存在一种在语义上更合适的方式来构造代码(即使代码结构更紧密地表达您的意图)。
对于您自己的情况,请问自己为什么每次循环都需要重置该值。 似乎您只是在使用对象在循环中的每次迭代中保存一些中间数据,并且您不必担心循环外的值(但如果您愿意,则需要使用riderBill的答案 )。 所以确实,您的ourVector
实例仅在该循环范围内有用。
因此,使您的代码结构反映出:
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
...
}
换句话说,只需在它所属的循环中声明它即可。 从语义上讲,这很有意义(它表示您实际使用该对象的方式和位置),并且无需修改ourVector
即可完成您想要的ourVector
。
通常,作为
初学者的
经验法则,请尝试在仍适用于您的代码的尽可能狭窄的范围内声明变量。
仅在实例化对象时才调用构造函数。 您不能显式调用它来重新初始化变量。 但是您可以从构造函数中调用public成员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) {}
此代码有效:
class ourVector
{
public:
ourVector() : vectorCap(20), vectorSize(0) { };
ourVector(int c, int s) : vectorCap(c), vectorSize(s) { };
void setVCap(int v)
{
vectorCap = v;
}
void setVSize(int v)
{
vectorSize = v;
}
int getVCap()
{
return vectorCap;
}
int getVSize()
{
return vectorSize;
}
void print()
{
std::cout << vectorCap << ' ' << vectorSize << '\n';
}
private:
int vectorCap;
int vectorSize;
};
int main()
{
ourVector vectorTest(5,5);
vectorTest.print();
vectorTest.setVCap(6);
vectorTest.setVSize(6);
vectorTest.print();
std::cout << vectorTest.getVCap() << ' ' << vectorTest.getVSize() << '\n';
vectorTest = ourVector();
vectorTest.print();
}
ourVector() : vectorCap(value), vectorSize(value) { };
零件是初始化器。 vectorCap(value)
部分将vectorCap
设置为value
; { }
部分是一个空方法,使用它可以进行所需的任何计算和验证。 可以调用SetVCap(int)
和SetVSize(int)
方法分别更改vectorCap
和vectorSize
的值。 getVCap()
和getVSize()
返回vectorCap和vectorSize的值。
在我的示例中,您不需要验证代码,因此初始化程序ourVector() : vectorCap(value), vectorSize(value) { };
工作完美。 如果需要验证输入,则可能希望从初始化程序中调用赋值函数,因此只需要执行一次验证,这使调试更加容易。 为此,只需将这些构造函数替换为这些构造函数即可。 现在,您只需要在一个地方验证输入:
ourVector()
{
setVCap(20);
setVSize(0);
}
ourVector(int c, int s)
{
setVCap(c);
setVSize(s);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.