我有两个版本的 C++ 代码。一个给出了问题,另一个则没有:
/*
* This compiles fine
*/
class base {
private:
const char c;
};
int main() {
base b(); // compiles fine
}
Run Code Online (Sandbox Code Playgroud)
/* * 这会产生编译错误 */
class base {
private:
const char c;
};
int main() {
base b; // error: structure 'b' with uninitialized const members
}
Run Code Online (Sandbox Code Playgroud)
请注意,区别在于“base b()”和“base b”。我认为两者都会调用默认构造函数,并且由于该类有一个 const 字段,因此程序将无法编译。请帮忙解释一下这一点。
我有一个类,我正在使用参数化构造函数创建它的一个对象。在此期间,参数化构造函数和默认构造函数都已被调用。
这是我的片段:
class student {
string name;
int age;
public:
student() {
cout << "Calling the default constructor\n";
}
student(string name1, int age1) {
cout << "Calling the parameterized const\n";
name = name1;
age = age1;
}
void print() {
cout << " name : " << name << " age : " << age << endl;
}
};
int main()
{
map<int, student> students;
students[0] = student("bob", 25);
students[1] = student("raven", 30);
for (map<int, student>::iterator it = students.begin(); it …Run Code Online (Sandbox Code Playgroud) c++ class stdmap default-constructor parameterized-constructor
来自 JavaScript 和 Python,我试图了解 C++ 类构造函数的细微差别和目的。
在下面的示例中,为什么允许在没有构造函数的情况下初始化属性?
class MyClass {
public:
int a = 1;
int b = 2;
};
Run Code Online (Sandbox Code Playgroud)
默认构造函数是否包括上述定义/初始化?下面两个例子有什么区别?:
1.
class MyClass {
public:
int a;
int b;
MyClass(){
a = 1;
b = 2;
}
};
Run Code Online (Sandbox Code Playgroud)
class MyClass {
public:
MyClass(){
// Some kind of variable declaration and definition like:
// this.a = 1;
// this.b = 2;
}
};
Run Code Online (Sandbox Code Playgroud)
对我来说,在没有构造函数的情况下进行初始化的选项听起来有点矫枉过正,而且令人困惑。在 Python 和 JavaScript 中,通常从构造函数声明和初始化所有变量,并且仅从构造函数开始。
这里的最佳做法是什么?
c++ constructor class constructor-overloading default-constructor
给定两个具有不同构造函数的类:
#include <iostream>
struct A {
int x;
A() {};
};
struct B {
int x;
B() = default;
};
int main() {
int x = 5;
x = 7;
printf("before: %d\n", x);
new(&x) A();
printf("%d\n", x);
new(&x) B();
printf("%d\n", x);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
before: 7
7
0
Run Code Online (Sandbox Code Playgroud)
为什么defaultctor 会进行零初始化int x?
在下面的示例中,struct A没有默认构造函数。因此,从它继承的struct B和 都struct C无法获得编译器生成的默认构造函数:
struct A {
A(int) {}
};
struct B : A {
B() = default; //#1
};
struct C : A {
C();
};
C::C() = default; //#2
Run Code Online (Sandbox Code Playgroud)
#1. 在 中struct B,默认的默认构造函数在类内部声明,并且所有编译器都接受它,只有 Clang 显示警告说:explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
#2. 但是在外部声明的默认构造函数struct C会生成编译器错误。演示: https: //gcc.godbolt.org/z/3EGc4rTqE
为什么默认构造函数的位置很重要:在类内部还是外部?
即使使用默认构造函数仍然有错误.
class Foo {
public:
Foo ( int x, int y, int type );
}
Run Code Online (Sandbox Code Playgroud)
并在.cpp文件中
Foo::Foo ( int x = 0, int y = 0, int type = 0 ) {
Run Code Online (Sandbox Code Playgroud)
然而,当我打电话给它
Foo foo_array[5][5];
Run Code Online (Sandbox Code Playgroud)
我收到了错误.这可能是什么原因?
更明确一点,当我在使用()创建对象时尝试访问实例变量时出现编译时错误,但是当我不这样做时,代码将按预期编译并运行.此外,此问题仅适用于默认构造函数.我想了解原因.
using namespace std;
#include <iostream>
class Student {
public:
int gpa;
Student() {
gpa = 4;
}
Student( int x ) {
gpa = x;
}
};
int main() {
Student zero;
Student sally( 2 );
Student jack();
cout << zero.gpa << endl; //prints 4
cout << sally.gpa << endl; // prints 2
cout << jack.gpa << endl; //error: request for member 'gpa' in 'jack', which is of non-class type 'Student()'
}
Run Code Online (Sandbox Code Playgroud) 我在python初始化程序中有一些背景知识(本质上是Python对象构造函数语法),在Python中实例化对象的语法如下:
class Account:
def __init__(self,name=None,address="Not Supplied",balance=0.0):
this.name = name
this.address=address
this.balance=balance
Run Code Online (Sandbox Code Playgroud)
为什么,在C#中,我必须提供默认的构造函数方法的机构,在蟒蛇的时候,我可以声明为可选的,默认值是在通过(见__init__的签名):
public class Account
{
private string name;
private string address;
private decimal balance;
public Account (string inName, string inAddress, decimal inBalance)
{
name = inName;
address = inAddress;
balance = inBalance;
}
public Account (string inName, string inAddress)
{
name = inName;
address = inAddress;
balance = 0;
}
public Account (string inName)
{
name = inName;
address = "Not Supplied";
balance = 0;
}
} …Run Code Online (Sandbox Code Playgroud) c# python constructor default-constructor object-initialization
我有一个MousableActor扩展具体类的抽象类Actor:
public abstract class MousableActor extends Actor
{
/**
* Constructs a MousableActor.
*/
protected void MousableActor()
{
}
}
Run Code Online (Sandbox Code Playgroud)
当我查看为该类生成的javadoc时,我看到一个公共的无参数构造函数:

根据JLS第8.8.9节:
如果类不包含构造函数声明,则隐式声明没有形式参数且没有throws子句的默认构造函数.
我一直认为这是一个if-and-only-if.为什么即使我显式声明了受保护的构造函数,也会创建公共默认构造函数?它是否与具有公共no-args构造函数的超类有关?
我在Java 1.8.0之上使用Greenfoot版本2.4.2(这应该不重要).