以下代码产生编译时错误:
'
base::print':无法访问类中声明的私有成员'base_der'
但是,我已public在派生类中创建了该成员.为什么这不起作用?
#include <iostream>
using namespace std;
class base
{
public:
int i;
void print(int i)
{
printf("base i\n");
}
};
class base_der : private base
{
public:
using base::print;
};
int main()
{
// This works:
base_der cls;
cls.print(10);
// This doesn't:
void (base_der::* print)(int);
print = &base_der::print; // Compile error here
}
Run Code Online (Sandbox Code Playgroud) c++ member-function-pointers member-pointers implicit-conversion private-inheritance
我正在阅读关于继承的内容,我有一个我几个小时都无法解决的重大问题:
鉴于一个类Bar是一个具有virtual函数的类,
class Bar
{
virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)
有什么不同:
class Foo : public Bar
{
virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)
和
class Foo : public virtual Bar
{
virtual void Cook();
};
Run Code Online (Sandbox Code Playgroud)
?谷歌搜索和阅读的时间提供了大量有关其用途的信息,但实际上没有人告诉我两者之间有什么区别,只是让我更加困惑.
当我从基础构造函数调用虚函数时,编译器不会给出任何错误.但是当我从基类构造函数调用纯虚函数时,它会产生编译错误.
考虑下面的示例程序:
#include <iostream>
using namespace std;
class base
{
public:
void virtual virtualfunc() = 0;
//void virtual virtualfunc();
base()
{
virtualfunc();
}
};
void base::virtualfunc()
{
cout << " pvf in base class\n";
}
class derived : public base
{
public:
void virtualfunc()
{
cout << "vf in derived class\n";
}
};
int main()
{
derived d;
base *bptr = &d;
bptr->virtualfunc();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里可以看出纯虚函数有一个定义.我期望在bptr->virtualfunc()执行时调用基类中定义的纯虚函数.相反,它给出了编译错误:
错误:从构造函数调用的抽象虚拟`virtual void base :: virtualfunc()'
这是什么原因?
c++ constructor virtual-functions compiler-errors dynamic-binding
鉴于以下两个构造函数签名,是否可以构造一个Couplewith Couple("George", "Nora")?我的编译器抱怨下面显示的错误.如果我用Couple(std::string("George"), std::string("Nora"))它调用它编译好.我猜这是一个隐式转换的问题让我感到惊讶,因为我虽然char*to string也没关系.
class Person
{
public:
Person(const std::string& name);
};
class Couple
{
public:
Coordinate(const Person& p1, const Person& p2, const Optional<Person>& = Optional<Person>());
};
TestCouple.cpp:69: error: no matching function for call to `Couple::Couple(const char[7], const char[5])'
TestCouple.h:24: note: candidates are: Couple::Couple(const Person&, const Person&, const Optional<fox::Person>&)
Run Code Online (Sandbox Code Playgroud) 我发现了一张C-ISH C++代码,并问自己(略学术)的问题,有什么隐式类型转换发生在这里,以在到达bool该if要求?
int val;
if( (std::cin >> val) == 0 )
...
Run Code Online (Sandbox Code Playgroud)
我到目前为止:
std::cin >> val返回(参考)cin,因此istream&==接收cin和0作为操作数,即istream和int我认为没有bool operator==(istream&, int)可用的(也没有相应的成员函数istream),所以是否涉及转换?
为了清楚起见,程序员的意图是检查输入是否成功,即应该写入if(!(std::cin >> val)).
从一些关于模板专业化的幻灯片:
#include <iostream>
using namespace std;
template<class X>
X& min(X& a, X& b)
{
return a > b ? b : a;
}
int& min(int& a, int & b)
{
// rewrite of the function in the case of int:
cout << "int explicit function\n";
return a > b ? b : a;
}
/*
new syntax – the more appropriate way:
template<>
int& min<int>(int& a, int& b)
{
cout << "int explicit function\n";
return a > b ? b …Run Code Online (Sandbox Code Playgroud) 我发现了一些问题(比如这个)问什么[[carries_dependency]]是,这不是我在这里问的。
我想知道你什么时候不应该使用它,因为我读过的所有答案都让人觉得你可以把这段代码贴在任何地方,而且你会神奇地得到相等或更快的代码。一个评论说代码可以相等或更慢,但海报没有详细说明。
我想在任何函数返回或参数上使用 this 的合适位置是指针或引用,并且将在调用线程内传递或返回,并且不应在回调或线程入口点上使用它。
有人可以评论我的理解并详细说明一般的主题,何时以及何时不使用它?
c++ multithreading memory-model carries-dependency stdatomic
我已经读过创建或复制std :: shared_ptr涉及一些开销(参考计数器的原子增量等).
但是如何从它创建一个std :: weak_ptr:
Obj * obj = new Obj();
// fast
Obj * o = obj;
// slow
std::shared_ptr<Obj> a(o);
// slow
std::shared_ptr<Obj> b(a);
// slow ?
std::weak_ptr<Obj> c(b);
Run Code Online (Sandbox Code Playgroud)
我希望在一些更快的性能,但我知道共享指针仍然必须递增弱引用计数器..所以这仍然像将shared_ptr复制到另一个慢?
声明const全局变量已证明对确定API的某些功能参数很有用.例如,在我的API上,运算符的最小数值精度顺序为2; 因此,我宣布:
const int kDefaultOrderAccuracy{2};
Run Code Online (Sandbox Code Playgroud)
作为全球变量.将它作为static const描述这些运算符的类的公共数据成员会更好吗?一般来说,选择一个比另一个更好?
今天进入一个有趣的问题,我试图理解为什么.
考虑以下:
class Base
{
public:
Base(){}
~Base(){}
static void function1(){}
void function2()
{
int function1;
function1 = 0;
function1(); //<-compiler error
function1 = 1;
}
};
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
表观调用括号前的表达式必须具有(指向 - )函数类型
我想我明白为什么我收到这个错误:
当function1它本身被调用时function2(),它实际上是一个函数指针function1().
里面的范围function2,当int function1被宣布," function1变"阴影" function1函数指针".
当function1()在内部调用时function2(),它假定function1是变量并且给出错误.
通过调用Base::function1();内部来解决这个问题function2().
我的问题是:为什么编译器在声明时没有出错int function1;?这不应该被允许吗?
c++ ×9
c++11 ×2
constructor ×2
declaration ×2
boolean ×1
inheritance ×1
istream ×1
linkage ×1
memory-model ×1
name-hiding ×1
name-lookup ×1
overloading ×1
performance ×1
scope ×1
shared-ptr ×1
stdatomic ×1
templates ×1
weak-ptr ×1