可能的重复:
构造函数中这个奇怪的冒号成员语法是什么?
我很少在常规代码中遇到类中的单个冒号,例如:
A::member():b(),c()
{
}
Run Code Online (Sandbox Code Playgroud)
单冒号在这里的重要性是什么?为什么用在这里?有时是强制性的吗?如果是,在哪些情况下?
class A {
public:
A(int v) {
_val = v;
}
private:
int _val;
};
class B {
public:
B(int v) {
a = A(v); // i think this is the key point
}
private:
A a;
};
int main() {
B b(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译说:
test.cpp: In constructor ‘B::B(int)’:
test.cpp:15: error: no matching function for call to ‘A::A()’
test.cpp:5: note: candidates are: A::A(int)
test.cpp:3: note: A::A(const A&)
Run Code Online (Sandbox Code Playgroud)
我已经学习了Java,我不知道如何在C++中处理这个问题.搜索了几天,PLZ告诉我C++可以这样做吗?
这两个构造函数声明之间有什么区别:
class Fruit {
private:
int price;
public:
Fruit(int x): price(x)
{
}
};
Run Code Online (Sandbox Code Playgroud)
VS
class Fruit {
private:
int price;
public:
Fruit(int x)
{
price = x;
}
};
Run Code Online (Sandbox Code Playgroud)
在继承的情况下我见过的第一个.
据我所知,这不是一个重复的问题.如果你发现一个人可以随意关闭这个问题.
它说在重新声明时添加默认参数使这个构造函数成为默认构造函数。
我对它做了一些研究,但我只是不明白我需要做什么来解决这个问题。
struct Transaction{
int type;
int amount;
int to_from_type;
Transaction(int, int, int);
};
Transaction :: Transaction(int type=0, int amount=0, int etc=0)
{
this->type=type;
this->amount=amount;
this->to_from_type=etc;
}
Transaction :: Transaction(int type=0, int amount=0, int etc=0) //I am getting an error at this code and I cannot fix it.
{
this->type=type;
this->amount=amount;
this->to_from_type=etc;
}
Run Code Online (Sandbox Code Playgroud)
我不是 C++ 方面的专家,很想得到一些关于我的代码的帮助。
在C++中,假设我有一个具有以下构造函数的Person -
Person::Person(const string& nm, const string& id)
{
name = nm;
idNum = id;
}
Run Code Online (Sandbox Code Playgroud)
现在,我还有一个名为Student的Person子类,其中包含额外的数据成员major和gradYear.是否有必要让Student类的构造函数在初始化列表中调用基类Person的构造函数,如果是,为什么?
Student::Student(const string& nm, const string& id, const string& maj, int year)
: Person(nm, id){
major =maj;
gradYear =year;
}
Run Code Online (Sandbox Code Playgroud)
我不能像这样定义学生的构造函数 -
Student::Student(const string& nm, const string& id, const string& maj, int year)
{
Person(nm, id);
major =maj;
gradYear =year;
}
Run Code Online (Sandbox Code Playgroud) 任何人都可以在这行代码中向我解释这个奇怪的一点吗?
ClassA :: ClassA(std :: string aName):name(aName)
显然,这是该类的声明
class ClassA
{
public:
std::string name;
ClassA(std::string aName);
};
Run Code Online (Sandbox Code Playgroud)
奇怪的代码行出现在它的cpp文件中
ClassA::ClassA(std::string aName) : name(aName)
Run Code Online (Sandbox Code Playgroud)
这不是多态吗?但那么,它是什么?
我正在尝试实现与node.js集成的C++扩展.此扩展将在内部调用一些阻塞调用,因此它需要为node.js世界提供非阻塞接口.
如https://nodejs.org/api/addons.html中所述,有两种方法可以实现非阻塞回调:
a)通过使用JavaScript函数的简单回调.所以我的扩展必须生成一个线程并立即返回,让该线程调用阻塞代码,然后在返回时调用JavaScript回调.这似乎相对简单.
b)通过使用libuv库,如果我理解正确,将事件发布到node.js事件循环.我没有详细阅读过libuv文档,但实现起来似乎相当复杂.
我的偏好当然是a),但我不知道其含义是什么.如果从不同的线程调用回调是否有任何问题,从而将node.js标准方法限制为非阻塞IO?或者是否需要使用libuv来正确处理我的代码及其阻塞调用的线程?
非常感谢您的帮助.
来自Java,我对下面的代码有困难.在我的理解中,b仅在第3行声明但未实例化.
什么是在A类中创建B实例的教科书方式?
class A {
private:
B b;
public:
A() {
//instantiate b here?
}
};
Run Code Online (Sandbox Code Playgroud)
编辑:如果B没有默认构造函数怎么办?
在我的印象中,我似乎看到了类似的东西:
A:::b()
Run Code Online (Sandbox Code Playgroud)
但是我不记得它是不是c ++,也是在c ++中搜索过"三重冒号"之后但是在c ++中看起来很少有关于它的信息,有没有例子代码可以在代码中有3个冒号(:: :)有效的语法?
我想创建一种方法来向控制台输出添加颜色,其工作方式与std::left和类似std::setw()。我最终得到了下面的代码,它完全按照我想要的方式工作。我了解它是如何工作的,但我想澄清一些事情。
这是代码:
#include <iostream>
#include <Windows.h>
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
enum class color { blue = FOREGROUND_BLUE, green, cyan, red, purple, yellow, white, bright = FOREGROUND_INTENSITY };
class coutColor {
public:
WORD Color;
coutColor(color colorvalue) : Color((WORD)colorvalue) { }
~coutColor() { SetConsoleTextAttribute(hConsole, (WORD)7); }
};
std::ostream& operator<<(std::ostream& os, const coutColor& colorout) {
SetConsoleTextAttribute(hConsole, colorout.Color);
return os;
}
int main() {
std::cout << coutColor(color::green) << "This text is green!\n";
std::cout << color::red << "This text is red! …Run Code Online (Sandbox Code Playgroud)