在阅读了C++11和有关它的一般指导方针之后,我经常阅读有关如何使用类内初始化和聚合初始化的文章。
下面是一个似乎是“老”的做事方式的例子:
class Example
{
public:
// Set "m_x" to "x", "m_y" gets set to the default value of 5
Example(int x) : m_x(x), m_y(5)
{
}
private:
int m_x;
int m_y;
};
据我所知,这就是人们现在所建议的:
class Exa
我用的是GCC 4.8.2:
$ g++ --version
g++ (GCC) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我有这样的定义:
#pragma once
#in
我有一个父类,其中包含一个函数funcB(),我喜欢通过在这个函数中做一些更改来用一个更好的函数覆盖它。父类中的此函数调用同一类中的另一个私有函数。
示例代码:
class classA {
private function funcA() {
return "funcA called";
}
public function funcB() {
$result = $this->funcA();
return $result;
}
}
class ClassB extends ClassA {
public function
我在Swift中有一个包含Shape类的Shapes模块。我刚刚打开了一个新的游乐场,并将这个模块添加到源文件中。游乐场中的Square类可以继承Shapes模块中的Shape类。没有问题,但Shape类中的公共方法在theSquare类中被覆盖。
根据swift消息来源,在执行继承时,只有开放的成员函数才能被覆盖。
也许这是一个非常基本的问题,所以我已经为问这个问题感到抱歉了。
这里是一个示例游乐场代码,当Parent和Child定义在不同的模块中时,行为是相同的:
open class Parent {
public init() { print("Parent i
据我所知,下列声明是相同的:
A a1(1);
A a2=1;
标题:
class A
{
public:
A(int num){}
};
但是当使用引用时,它不会编译
class B{
private:
int m_a = 0;
int& m_b(m_a);
public:
B(int num):
{
}
};
got编译错误,声明m_a不是类型名称,但是当使用=执行此操作时,它会工作:
class B
{
private:
int m_a = 0;
int&am
class Base{
public:
virtual void show(){
cout<<" In Base \n";
}
};
class Derived: public Base
{
int x;
public:
void show() {
this->x = 10;
this->printX();
}
void printX(){
cout<<x;
我有一个Enum和简单的基类,如下所示,
enum E
{
a,
b
};
template<E e>
struct Base
{
static int i;
};
以及它的孩子们
struct CC
{
template<E e, class T>
struct C;
template<class T>
struct C<a, T> : Base<a>
{
void Do(int i)
类内定义是否违反ODR规则?
Bjarne的声明如下:
类通常在头文件中声明,而头文件通常包含在许多翻译单元中。然而,为了避免复杂的链接器规则,C++要求每个对象都有一个唯一的定义。如果C++允许在类中定义需要作为对象存储在内存中的实体,则该规则将被打破。
class foo
{
float f = 1.1f;
int i = 1;
string s = "foo";
long long ll = 44ll;
};
这是议员的实际定义吗?这违反了ODR规则吗?如果没有,通过构造函数进行初始化的好处是什么?
我本打算对GCC提出一个bug,但后来意识到如果我对标准的解释是正确的,那么这是一个核心语言缺陷,而不是编译器bug。
当在类范围外定义数组类型的静态数据成员时,将在类范围内查找数组绑定中的标识符。
§9.4.2 class.static.data说“静态数据成员的定义中的初始化器表达式在它的类(3.3.7)的作用域中”,但没有说任何关于声明符本身的内容。这似乎是声明器中唯一的名称查找上下文。
§8.4.2 dcl.array没有提到数组绑定的作用域,因此默认情况下,表达式在封闭作用域中求值,这是命名空间。
class X {
static int const size = some_co
struct X
{
void f() noexcept(noexcept(g()));
void g() noexcept;
};
在vc++和clang中,这是可行的,但gcc却在抱怨:
source_file.cpp:6:34: error: ‘g’ was not declared in this scope
void f() noexcept(noexcept(g()));
^
我认为这是gcc的一个错误,而不是其他人的一个特征。对吗?
我有一个简单的代码:
#include <type_traits>
class A {
public:
static int a;
};
void a() {}
int A::a = [](){static_assert(std::is_function<decltype(a)>::value,"'a' is not a function");return 777;}();
int main() {
return 0;
}
在编译期间(使用g++ 4.8.1和clang3.4),关于'a‘的get静态断言
如果我公开地在基类中typedef某些类型,那么C++标准是否保证typedefed类型对于派生类总是可见的?
例如:
struct A
{
typedef int T;
};
struct B : A
{
void f(T) {}; // Does the C++ standard guarantee it can be compiled?
};
int main()
{
B().f(8);
}
我通过一些简单的例子来加深我对私有静态成员的理解。然而,我得到了编译错误。
下面是我下面的例子:
我已经更改了c++版本,看看它是否与c++版本有关。
class test1
{
static int i;
static int j;
static int k;
static int f() { return 0;}
int p;
public:
test1()
{
p = 0;
}
};
int main(void)
{
test1 t;
int test1::i = test1::f();
int test1::j = te
我试图在类范围内初始化成员vec,但是编译器会抛出一个更神秘的错误。
class A {
public:
static const size_t sz = 10;
static const std::vector<double> vec{ sz }; // error
};
编译器(gcc)给出以下错误:
error: in-class initialization of static data member 'std::vector<double> A::vec' of non-literal type
10 | stat
我总是很难找到为什么我在代码中得到“对静态变量的未定义引用”,最后总是遇到以下问题:
我知道我需要在类声明之外定义数据。
在本例中:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initi
我正在做一个n吨的基类模板。我还不担心懒惰,所以我的意图是:
确保类只有n个实例,并提供对它们的全局访问点。
到目前为止,我的代码如下:
template<typename Derived, size_t n = 1>
class n_ton_base // Singletons are the default
{
static Derived instances[n + (n == 0)];
// Zerotons are supported, too
protected:
我开始学习C++和QT,但有时我从一本书中粘贴的最简单的代码会导致错误。 我在用g++4.4.2在Ubuntu 10.04上使用QtCreatorIDE。g++编译器语法和其他编译器之间有什么区别吗?例如,当我试图访问静态成员时,总会出现问题。 #include <iostream>
using namespace std;
class A
{
public:
static int x;
static int getX() {return x;}
};
int main()
{
int A::x = 100; // error: invalid
我写了以下代码:
class A
{
public:
int foo();
};
class B
{
public:
int A::foo(){ return 0; }; //error: non-friend class member 'foo' cannot have a qualified name
};
int main(){ }
为什么会导致这个错误?你能从标准中得到明确拒绝这种解释的引用吗?
我在从文本字段读取值时遇到了困难。每次输入这行let username = UsernameTextField.text时,我都会看到一个错误:“不能在属性初始化器中使用实例成员'UsernameTextField‘;属性初始化器在可用'self’之前运行”。
以下是完整的代码:
import UIKit
class SignInViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var UsernameTextField: UITextField!
@IBOutlet var Pa
我知道我们可以为struct成员设置默认值。例如,我可以为这些成员设置默认值:
struct Foo {
int a = 0;
int b = 1;
int c;
}
假设我有另一个c成员的构造函数:
struct Foo {
int a = 0;
int b = 1;
int c;
foo(int input_c): c(input_c) {}
}
在这种情况下,当我构造Foo时,构造的顺序是什么?如果我做了
Foo(100)
我的理解是,a和b都是默认构造的,然后c被指定为100,对吗?
我的一部分困惑也是执行的顺序。对于默认值,它是否已在构造函数之前执行?
struct test
{
void f() {};
};
test t1;
using memfun_t = void (test::*)();
memfun_t mf = &test::f;
auto a1 = &test::f; // OK
auto a2 = t1.*mf; // error
auto a3 = &(t1.*mf); // still no luck
有什么不可以推断的吗?我希望得到引用标准的答案。
编辑:
我找到了一个名为__closure的RAD语言扩展,它似乎解决了这个问题。
class base {
public: