我需要知道如何让事情发挥作用.我有一个带有构造函数的类,并在初始化列表中初始化了一些常量.我想要的是能够创建一个不同的构造函数,它需要一些额外的参数,但仍然使用初始化列表.像这样:
class TestClass
{
const int cVal;
int newX;
TestClass(int x) : cVal(10)
{ newX = x + 1; }
TestClass(int i, int j) : TestClass(i)
{ newX += j; }
}
Run Code Online (Sandbox Code Playgroud)
完全可怕的例子,但它得到了重点.问题是,我如何让这个工作?
这可能吗?例如,如果我写
Car myCar;
Run Code Online (Sandbox Code Playgroud)
然后调用不带Car参数的构造函数.如果只有构造函数接受参数,则会导致错误.
在Java中,我可以使用与上面完全相同的语句轻松声明一个对象并在以后创建它.
请考虑以下示例代码:
#include <iostream>
using namespace std;
class core
{
public:
core(const core& obj)
{
cout << "core copy ctor called\n";
}
core()
{
cout << "core default ctor called\n";
}
};
class sample : public core
{
public:
sample()
{
cout << "sample default ctor called\n";
}
#if 0
sample(const sample& obj)
{
cout << "sample copy ctor called\n";
}
#endif
};
int main()
{
sample s1;
sample s2 = s1; //Line1
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Type1:未为类示例显式声明的复制构造函数
(Type1显示在上面的代码中.然后编译器隐式生成类sample的复制构造函数).Line1执行语句时,首先class …
c++ copy-constructor default-constructor implicit-declaration
我需要在我的类中有一个复制构造函数,因为我需要创建重复的对象.我相信如果我将创建一个复制构造函数,我将不得不指定非参数化构造函数,因为Java将不再提供默认构造函数.
我不想触摸默认构造函数,因为这是我在代码中使用的地方.有没有一个复制构造函数或类似的东西没有定义基本构造函数的解决方法.
码:
struct A
{
~A(){ };
};
A::A(){ }; //error: definition of implicitly declared default constructor
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
为什么代码会产生错误?我希望程序编译好.标准说N3797::12.8/7 [class.copy]:
如果类定义没有显式声明复制构造函数,则会隐式声明一个.如果类定义声明了移动构造函数或移动赋值运算符,则隐式声明的复制构造函数被定义为已删除; 否则,它被定义为默认值(8.4).如果 类具有用户声明的复制赋值运算符或用户声明的 析构函数,则不推荐使用后一种情况.
这是一个错误或我的误解?
在我私下继承我的类之前,我想使用C++ 11使我的类不可复制boost::noncopyable.在C++ 11中,我实现了以下内容:
class Foo
{
public:
Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;
};
Run Code Online (Sandbox Code Playgroud)
通过此更改编译客户端代码(使用VS 2013)会出现以下错误:
..\main.cpp(9): error C2512: 'Foo' : no appropriate default constructor available
Run Code Online (Sandbox Code Playgroud)
我的客户端代码非常简单:
int main()
{
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何C++ 11规则在我的情况下隐式删除默认构造函数?
我有一个看起来像这样的课程:
Person(int pID,
int zipCode,
float ySalary,
const string& fName,
const string& mName,
const string& lName)
Run Code Online (Sandbox Code Playgroud)
当我尝试创建一个默认构造函数时,如下所示:
Person::Person(void){
zipCode = NULL;
pID = NULL;
ySalary = NULL;
fName = "";
mName = "";
lName = "";
}
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,说没有运算符"="匹配const std :: string = const char [1];
我正在使用Visual Studio 2013.
我有以下代码
class Foo{
public:
Foo(){};
Foo* fPtr;
float f;
};
int main()
{
Foo foo1; // No Warning. fPtr is initialized to NULL, f is set to 0.0.
Foo foo2(foo1); // Compiles fine. Uses compiler generated copy constructor
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在考虑相同的代码,但没有用户定义的默认构造函数.
class Foo{
public:
// No user defined default constructor, or equivalently
// Foo() = default;
Foo* fPtr;
float f;
};
int main()
{
Foo foo1; // Neither fPtr nor f are initialized, both contain garbage. …Run Code Online (Sandbox Code Playgroud) 我有一个基类和派生的基类:
class Neuron
{
protected:
double input;
double output;
};
class InputNeuron : public Neuron
{
public:
InputNeuron();
};
Run Code Online (Sandbox Code Playgroud)
派生类的默认构造函数定义如下
InputNeuron::InputNeuron() : input(0.0), output(0.0) {}
Run Code Online (Sandbox Code Playgroud)
问题是:输入和输出的初始化是错误的.
我的目标是从继承中获益,以避免在派生类中重新声明输入和输出.但是,在当前状态下,使用这些成员会发出一条消息:input is not a nonstatic data member or base class of class InputNeuron消息我似乎无法从中获取信息.
让我们假设以下类Foo。
struct Foo
{
int i;
};
Run Code Online (Sandbox Code Playgroud)
如果要创建此类的实例并对其进行初始化,则应该执行以下操作:Foo foo1 = Foo();调用构造函数。
int main(void)
{
foo1 = Foo();
cout << foo1.i << " : " << foo1.j << endl; // " 0 "
}
Run Code Online (Sandbox Code Playgroud)
然后我以为我会用以下代码行调用默认构造函数,但事实并非如此:
int main(void)
{
Foo foo2(); // most vexing parse
cout << foo2 << endl; // " 1 " ??? why?
foo2.i++; // error: request for member ‘i’ in ‘foo2’, which is of non-class type ‘Foo()’
}
Run Code Online (Sandbox Code Playgroud)
为什么foo2初始化为int(1)而不是Foo()?
我知道最烦人的解析,它告诉我,当我将一行解释为一个函数时,它就被解释为一个函数。
但是foo1()被解释为一个int,而不是一个函数,也不是一个Foo类。
之所以编译Foo foo2()行是因为它将其解释为函数原型。好。
为什么要编译此行? cout << …
c++ constructor struct default-constructor most-vexing-parse