此常见问题解答涉及聚合和POD,并涵盖以下材料:
编辑:这个问题出现了,我想我已经开始了!去StackOverflow !! :d
我的考试即将开始,去年考试的一个问题是通过实施以下构造函数来发现问题,并编写一个更正的问题.
Rectangle::Rectangle(string col, int len, int br)
{
setColour(col);
length =len;
breadth=br;
}
Run Code Online (Sandbox Code Playgroud)
类定义如下:
class Polygon
{
public:
Polygon(string col="red");
void printDetails(); // prints colour only
virtual double getArea()=0;
void setColour(string col);
private:
string colour;
};
class Rectangle : public Polygon
{
public:
Rectangle(string, int, int);
void printDetails(); // prints colour and area
// for part 3, delete the line below
double getArea();
private:
int length;
int breadth;
};
Run Code Online (Sandbox Code Playgroud)
我已将代码写入编译器,运行正常.我猜这个问题与继承有关,因为它string colour;是私有的,但是setColour是公开的,所以我无法弄明白.除非它Rectangle::Rectangle(string …
我正在尝试使用默认值创建构造函数.复杂性来自于为类使用单独的头文件和代码文件.我有一个包含以下内容的头文件:
class foo {
bool dbg;
public:
foo(bool debug = false);
}
Run Code Online (Sandbox Code Playgroud)
一个代码文件包含:
foo::foo(bool debug = false) {
dbg = debug;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用g ++(即g++ -c foo.cc)编译时,它会给出一个错误:
foo.cc:373:65: error: default argument given for parameter 1 of ‘foo::foo(bool)’
foo.h:66:4: error: after previous specification in ‘foo::foo(bool)’
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我写了一个模板类,它给出了编译错误
template<class T>
class Entity
{
string EntityName;
int EntitySize;
Entity<T*> pPrev;
Entity<T*> pNext;
public:
Entity<T>(const string & name, int size)
{
EntityName = name;
EntitySize = size;
}
//member functions
};
Run Code Online (Sandbox Code Playgroud)
我正在使用MSVC++ 2008,错误是:
致命错误C1202:递归类型或函数依赖关系上下文过于复杂
我没有在班上写过任何递归函数.那么为什么这个错误?请帮忙.
这编译,但我从来没有在任何其他代码中看到它.安全吗?
Testclass():sources(new int[32]){}
Run Code Online (Sandbox Code Playgroud)
代替:
Testclass(){
sources = new int[32];
}
Run Code Online (Sandbox Code Playgroud) 我是C++的新手,以及课程的全部理念 - 我还在读一本书来尝试和学习.我正在阅读的这本书说,当我构建一个类时,我可以通过这样做来指定默认值:
class foo {
public:
foo(char c, int i);
private:
char exampleChar;
int exampleInt;
};
foo::foo(char c, int i):
exampleChar(c),
exampleInt(i)
{}
Run Code Online (Sandbox Code Playgroud)
这段代码(对我来说)看起来非常混乱,并且不遵循我在其他语言中习惯的规则.我的问题是,做上述内容与此之间的区别是什么(下面,我个人认为看起来更清洁)?
foo::foo(char c, int i) {
exampleChar = c;
exampleInt = i;
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑的一些事情是:如果大规模地进行性能/效率问题 - 还是完全一样?
我试图了解以下类之间的真正区别。
class A
{
public:
A()
:width(500)
,height(300){};
int width;
int height;
};
class B
{
public:
B()
{
width = 500;
height = 300;
};
int width;
int height;
};
class C
{
public:
int width = 500;
int height = 300;
};
Run Code Online (Sandbox Code Playgroud)
您认为在类中初始化width和height变量的最佳方法是哪一种?
我应该坚持一种方式而不是其他方式吗?
变量有什么问题international_standard_book_number?我怎样才能让它改变,无论何时isbn_field_i改变?
#include <iostream>
#include <string>
class ISBN
{
private:
unsigned int isbn_field_1 = 0;
unsigned int isbn_field_2 = 0;
unsigned int isbn_field_3 = 0;
char digit_or_letter = 'a';
std::string international_standard_book_number =
std::to_string(isbn_field_1) + "-" + std::to_string(isbn_field_2) + "-" +
std::to_string(isbn_field_3) + "-" + digit_or_letter;
public:
ISBN()
{
isbn_field_1 = 0, isbn_field_2 = 0, isbn_field_3 = 0, digit_or_letter = 'a';
}
ISBN(unsigned int a, unsigned int b, unsigned int c, char d)
{
isbn_field_1 = a, isbn_field_2 …Run Code Online (Sandbox Code Playgroud) 我刚刚浏览了Cprogramming.com上的随机页面,并注意到了Constructors and Destructors教程/示例页面.他们使用了以下定义构造函数的方法:
class String
{
private:
char *str;
int size;
public:
String() : str(NULL), size(0) { } // <- This statement
String(int size) : str(NULL), size(size) { // <- And this one
str = new char[size];
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用带有魔术this指针的构造函数的老式定义:
String() {
this->str = NULL;
this->size = 0;
}
String(int size) {
this->size = size;
this->str = new char[size];
}
Run Code Online (Sandbox Code Playgroud)
除了明显较小的代码(较少的行数)之外,第一个声明中是否还有其他好处?
PS:自从我上次用C++编写内容以来已经有好几年了.
c++ ×10
constructor ×4
class ×3
c++17 ×2
aggregate ×1
c++-faq ×1
c++11 ×1
coding-style ×1
templates ×1
types ×1