我正在更改一个过去采用整数参数的旧例程,以便它现在采用对象的const引用.我希望编译器能告诉我调用函数的位置(因为参数类型错误),但是对象有一个构造函数,它接受一个整数,所以编译器创建一个临时对象,而不是失败,传递给它整数,并将对它的引用传递给例程.示例代码:
class thing {
public:
thing( int x ) {
printf( "Creating a thing(%d)\n", x );
}
};
class X {
public:
X( const thing &t ) {
printf( "Creating an X from a thing\n" );
}
};
int main( int, char ** ) {
thing a_thing( 5 );
X an_x( 6 );
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我希望该X an_x( 6 )行不能编译,因为没有X构造函数需要int.但它确实编译,输出如下:
Creating a thing(5)
Creating a thing(6)
Creating an X from a thing …Run Code Online (Sandbox Code Playgroud) 如果这是一个骗局,我道歉.我发现了一些帖子.防止隐式转换,但没有任何重新.鼓励隐含的结构.
如果我有:
class Rect
{
public:
Rect( float x1, float y1, float x2, float y2){};
};
Run Code Online (Sandbox Code Playgroud)
和自由功能:
Rect Scale( const Rect & );
Run Code Online (Sandbox Code Playgroud)
为什么会
Rect s = Scale( 137.0f, 68.0f, 235.0f, 156.0f );
Run Code Online (Sandbox Code Playgroud)
不做隐式构造a const Rect&而是生成此编译器错误
'Scale' : function does not take 4 arguments
Run Code Online (Sandbox Code Playgroud) #include <iostream>
class base
{
public:
virtual void print (int a)
{
std::cout << "a: " << a << " base\n";
}
virtual void print (int a, int b)
{
std::cout << "base\n";
}
};
class derived : public base
{
public:
virtual void print (double d)
{
std::cout << "derived\n";
}
};
int main ()
{
int i = 10;
double d = 10000.0;
base *b = new derived ();
b->print (i, i);
b->print (d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该函数的输出是: …
我想在我的代码中清楚地区分3D和2D点.显而易见的解决方案是拥有单独的类.
另一方面,从z = 0到2D点的3D点的转换非常常见.因此,我想使用一个公共基类,这样我就可以在内存中进行这些转换.为了明确区分类型,我想禁止隐式转换为该基类.那可行吗?
或者是否可能有不同的方法来创建具有类似功能的不同类型?
我正在测试这段代码并想知道为什么在编译时没有失败?我正在使用c ++ 11和g ++ 4.7.2.
我的生产代码上有类似的结构,它在运行时给出错误,然后我发现我正在用错误的参数类型构造类.
#include <iostream>
#include <vector>
typedef std::vector<std::string> Word;
class Data {
public:
const Word &word;
Data(Word w) : word(w) {}
};
class Base{
const Data &data;
public:
Base(const Data &d): data(d) {}
~Base() {}
};
class Work : public Base{
public:
Work(const Data &d): Base(d){}
~Work() {}
};
int main(int argc, char **argv){
Word words;
words.push_back("work");
/*
* I'm confused with this constructor, why this passed the compilation
* ??
* Any special rule to …Run Code Online (Sandbox Code Playgroud) class P {
public:
explicit P( int a, int b, int c) {
std::cout<<"calling explicit constructor"<<"\n";
}
};
int main() {
P z {77,5,42}; // OK
P w = {77,5,42}; // ERROR due to explicit (no implicit type conversion allowed)
}
Run Code Online (Sandbox Code Playgroud)
我认为 {77,5,42}有隐式类型std::initialization_list<int>。如果是这种情况,是什么导致变量构造失败z?
也许这是一个简单的问题,因为我还是C++的新手.我想使用某种工厂来封装我的应用程序中的日志记录.这个想法是只有工厂知道哪个具体类将处理函数调用.应用程序将始终调用基本日志记录类的抽象接口.
工厂方法应如下所示:
std::unique_ptr<AbstractLoggingClass> Factory::getDefaultLogger(const std::string& param){
return new ConcreteLoggingClass(param);
}
Run Code Online (Sandbox Code Playgroud)
ConcreteLoggingClass是.的子类AbstractLoggingClass.
但是我收到以下错误:
Error: could not convert '(operator new(64ul), (<statement>,
((ConcreteLoggingClass*)<anonymous>)))' from 'ConcreteLoggingClass*'
to 'std::unique_ptr<AbstractLoggingClass>'
Run Code Online (Sandbox Code Playgroud)
我的问题是,我不知道如何实例化ConcreteLoggingClass并返回unique_ptr到AbstractLoggingClass
我已经找到了这篇文章,但我仍然没有看到解决方案.
class Test{
public :
int x;
Test()
{
x = 0;
cout<<"constructor with no arguments called"<<endl;
}
Test(int xx)
{
x = xx;
cout<<"constructor with single int argument called"<<endl;
}
};
int main()
{
Test a(10);
Test aa = 10;
}
Run Code Online (Sandbox Code Playgroud)
输出:程序编译和输出
带有单个int参数的构造函数
带有单个int参数的构造函数
但现在
class Test{
public :
int x;
Test()
{
x = 0;
cout<<"constructor with no arguments called"<<endl;
}
Test(int xx)
{
x = xx;
cout<<"constructor with single int argument called"<<endl;
}
Test( Test& xx)
{
x …Run Code Online (Sandbox Code Playgroud) 我看到有时这个构造函数用内联显式编写.例如:
protected :
inline explicit Singleton() {
CCASSERT(Singleton::instance_ == 0, "error Singleton::instance_ == 0.");
Singleton::instance_ = static_cast<T*>(this);
}
inline ~Singleton() {
Singleton::instance_ = 0;
}
Run Code Online (Sandbox Code Playgroud)
内联显式有什么用?
为什么使构造函数显式不会阻止它被派生类隐式调用?
class A{
public:
explicit A(){}
};
class B : public A{
public:
B(){ //Constructor A() is called implicitly
//...
}
}
Run Code Online (Sandbox Code Playgroud)
在我的程序中遇到编译器错误时,我遇到了这种情况,这样可以省去很多时间来查找错误.现在我改变了A的默认构造函数来接受一个虚拟的"int"参数来实现它,但不应该"显式"关键字为此工作?
g ++ - 4.8编译上面的代码,没有任何错误或警告.