我正在尝试用C++创建一个需要多个对象构造函数的对象.说Foo()和Foo(int)地方Foo(int)然后调用Foo().简化代码如下:
#include <iostream>
class Foo{
private:
int iX;
public:
void printX(string sLabel){
cout << sLabel << " : " << " Foo::iX = " << Foo::iX << endl;
};
void setX(int iX){
Foo::iX = iX;
Foo::printX("setX(void) Method");
};
Foo(){
Foo::iX = 1;
Foo::printX("Foo(void) Constructor");
};
Foo(int iX){
Foo::setX(iX);
Foo::printX("Foo(int) Constructor");
Foo::Foo();
Foo::printX("Foo(int) Constructor");
};
};
int main( int argc, char** argv ){
Foo bar(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其输出是
setX(void) Method : Foo::iX …Run Code Online (Sandbox Code Playgroud) 假设您编写了一个类A,构造函数是私有的(以防止其他人在堆栈上创建它)然后有一天另一个开发人员添加一个新的ctor,比如A(int),并想在main()中使用:
A a(1)
在堆栈上创建它.你怎么防止这种情况?
我的解决方案
声明一个公共构造函数
A(void& input )
{
Cerr << “please do not create it on stack” << endl ;
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
我不确定它是否正确?
谢谢
这是一个关于 C++ 初始值设定项列表语法的问题。
是否可以从初始化列表中调用函数而不将它们作为成员对象构造函数的参数?
下面列出的代码示例是从工作中的类似情况转述(paracoded?)。
情况
编码
#include <iostream>
#define LOG { std::cout << __PRETTY_FUNCTION__ << std::endl; }
namespace
{
template <class T>
class SingletonService
{
public:
static T* Instance() { LOG; return mpT; }
static void InstallInstance(T* pT) { LOG; mpT = pT; }
static void DeleteInstance() { if (mpT) delete mpT; }
protected:
static T* mpT;
};
template <class T>
T* SingletonService<T>::mpT = NULL;
class OneOfMe
{
public:
OneOfMe() { LOG; };
virtual ~OneOfMe() { …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
class date{
public:
int month;
int day;
int year;
private:
date(int x, int y, int z);
public:
date(int x, int y);
};
date::date(int x, int y, int z): month{x}, day{y}, year{z} {
cout << "Hello you called me PRIVATE constructor" << endl;
}
date::date(int x, int y){
cout << "Hello you called me PUBLIC constructor" << endl;
date(x, y, 100);
}
int main(){
date x{11, 21};
cout << x.month << endl;
cout << x.day << …Run Code Online (Sandbox Code Playgroud) 我一直在盯着我的代码,我无法弄清楚为什么我的构造函数不会被调用.
它完全忽略了我的构造函数(我已经检查过调试器).
这是我的testapp:
using namespace MyEngine;
int _tmain(int argc, _TCHAR* argv[])
{
TestManager* testMgr = new TestManager();
testMgr->RunAllTests();
delete testMgr;
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
TestManager.h:
namespace MyEngine
{
class TestManager
{
public:
TestManager();
TestManager(uint64_t RepeatTimes);
~TestManager();
bool RunAllTests();
bool RunMemoryTests();
private:
Engine* mEngine;
ILogManager* mLogger;
MemoryTestManager* mMemTestMgr;
uint64_t mRepeatTimes;
};
}
Run Code Online (Sandbox Code Playgroud)
和TestManager.cpp
namespace MyEngine
{
TestManager::TestManager()
{
TestManager(1);
}
TestManager::TestManager(uint64_t RepeatTimes)
{
if (RepeatTimes>0)
mRepeatTimes = RepeatTimes;
else
{
mRepeatTimes = 1;
}
mEngine = Engine::GetEngine();
mMemTestMgr = new MemoryTestManager(); …Run Code Online (Sandbox Code Playgroud) 我试图使用委托的构造函数,并试图遵循这个问题和这个问题中找到的格式,但是,我仍然有问题.
我的player.h文件是这样的:
#ifndef PLAYER_H_
#define PLAYER_H_
#include <string>
class Player
{
public:
Player(void);
Player(std::string name, int score);
~Player();
protected:
std::string name_;
int score_;
};
#endif
Run Code Online (Sandbox Code Playgroud)
我的player.cpp文件是这样的:
#include "player.h"
Player::Player(std::string name, int score)
{
score_ = score;
name_ = name;
}
Player::Player(void) : Player(NULL,0)
{
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译时,我收到以下错误:
1>a:\projects\test\src\player.cpp(5): error C2614: 'Player' : illegal member initialization: 'Player' is not a base or member
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如果相关,我使用的是VS2012.
#include <cstdio>
struct A {
int a;
A() {
a = 2;
printf("Default\n");
}
A(int b_) {
a = 1;
if(b_ == 10) {
A();
}
}
};
int main(int argc, char **argv) {
A a(10);
printf("a=%d\n", a.a);
A b(11);
printf("b=%d\n", b.a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这打印:
Default
a=1
b=1
Run Code Online (Sandbox Code Playgroud)
也就是说,它在b_ == 10时进入默认构造函数,但在不是时则进入默认构造函数.但它不会更改aa中的值,即使它进入Default构造函数.
为什么?
我昨晚写了一些java,我有两个看起来基本相似的构造函数,除了我的默认构造函数为我的对象提供了一些值,它是这样的:
testObject(){
width=5;
height=12;
depth=7;
//other stuff is the same as the next one
}
testObject(int x, int y, int z){
width=x;
height = y;
depth = z;
//All the other stuff is the same as default
}
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,我能够转换代码来代替:
testObject(){
this(5,12,7);
}
Run Code Online (Sandbox Code Playgroud)
这将默认构造函数中的值作为要构建的3-int构造函数发送回构造函数.有没有办法在C++中获得这种类型的功能?
我试图创建一个调用默认构造函数的显式构造函数,但它说我没有.
class Paddle{
private:
int x, y;
int startX, startY;
public:
Paddle(){
x = y = 0;
}
Paddle(int posX, int posY) : Paddle(){ // <-- the error is on ": Paddle()"
startX = posX;
startY = posY;
x = posX;
y = posY;
}
};
Run Code Online (Sandbox Code Playgroud)
究竟是什么导致这种情况发生,我该如何解决?提前致谢!
可能重复:
构造函数中的c ++调用构造函数
如何在c ++中做"自我"(这个)作业?
Java的:
public Point(Point p) {
this(p.x, p.y);
}
Run Code Online (Sandbox Code Playgroud)
在C++中如何做到这一点?
它会是相似的this->(constructor of point that takes x, constructor of point that takes y);吗?
我在C++中有两个构造函数,一个构造函数调用另一个构造函数以便不复制初始化逻辑.
#include <iostream>
#include <memory>
using namespace std;
class A
{
int x;
int y;
public:
A(int x)
{
cout << this << endl;
this->x = x;
}
A()
{
cout << this << endl;
A(20);
}
...
};
Run Code Online (Sandbox Code Playgroud)
有趣的是A()调用A(int),但是this指针指向不同的地址.为什么是这样?或者是这个g ++错误?
int main(int argc, char *argv[]) {
A* a = new A();
}
0x7fa8dbc009d0 <-- from A()
0x7fff67d660d0 <-- from A(int)
Run Code Online (Sandbox Code Playgroud) 我现在已经在C++工作了一段时间,但我以前从未遇到过这个错误.我有一个结构(名为skew_value),它有一个初始化方法,所以一切都可以有正确的默认值(如果它有一个以上的构造函数和析构函数,我使它成为一个类而不是一个结构).我已经验证了构造函数确实被调用(断点).并且正确设置变量.但是一旦构造函数完成,一切都是未初始化的.
代码如下:
#ifndef _LBMOON_GRAPHICSYSTEM_SKEW_VALUE_H
#define _LBMOON_GRAPHICSYSTEM_SKEW_VALUE_H
struct skew_value
{
skew_value(float tlX=1, float tlY=0, float trX=0, float trY=0, float blX=0, float blY=0, float brX=0, float brY=0)
{
skew_value(Vector2f(tlX,tlY), Vector2f(trX,trY), Vector2f(blX,blY), Vector2f(brX,brY));
}
skew_value(Vector2f topLeft, Vector2f topRight, Vector2f bottomLeft, Vector2f bottomRight)
{
TLSkew = topLeft;
TRSkew = topRight;
BLSkew = bottomLeft;
BRSkew = bottomRight;
xScale = 1;
yScale = 1;
}
float xScale;
float yScale;
Vector2f TLSkew;
Vector2f TRSkew;
Vector2f BLSkew;
Vector2f BRSkew;
Vector2f TLOrigin;
Vector2f TROrigin;
Vector2f BLOrigin;
Vector2f BROrigin;
unsigned …Run Code Online (Sandbox Code Playgroud) c++ ×11
constructor ×8
c++11 ×2
class ×2
java ×2
object ×2
this ×2
g++ ×1
methods ×1
overloading ×1
private ×1
variables ×1
visual-c++ ×1