Stack Overflow教给我的很多东西都是所谓的"最令人烦恼的解析",经典地用一条线来证明
A a(B()); //declares a function
Run Code Online (Sandbox Code Playgroud)
虽然这对于大多数人而言,直观地看起来是a类型对象的声明A,将临时B对象作为构造函数参数,它实际上是一个函数a返回的声明A,将一个指针指向一个返回的函数,它B本身不带参数.同样的线
A a(); //declares a function
Run Code Online (Sandbox Code Playgroud)
也属于同一类别,因为它代替一个对象,它声明了一个函数.现在,在第一种情况下,这个问题的通常解决方法是在其周围添加一组额外的括号/括号B(),因为编译器会将其解释为对象的声明
A a((B())); //declares an object
Run Code Online (Sandbox Code Playgroud)
但是,在第二种情况下,执行相同操作会导致编译错误
A a(()); //compile error
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么?是的我非常清楚正确的"解决方法"是将其更改为A a;,但我很想知道()第一个示例中的额外功能是什么,然后在重新应用它时不起作用第二个例子.A a((B()));变通办法是否是写入标准的特定异常?
我的代码:
window.cpp
Window::Window(int w, int h, const char *title, const char *icon)
{
height = h;
width = w;
if(SDL_Init( SDL_INIT_EVERYTHING ) == 0)
{
SDL_WM_SetCaption(title, NULL);
SDL_WM_SetIcon(SDL_LoadBMP(icon),NULL);
screen = SDL_SetVideoMode(width, height, 32,
SDL_SWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
if(screen == NULL)
{
running = false;
return;
}
fullscreen = false;
}
else
running = false;
return;
}
Window::Window()
{
const SDL_VideoInfo* info = SDL_GetVideoInfo();
screenWidth = info->current_w;
screenHeight = info->current_h;
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");
}
Run Code Online (Sandbox Code Playgroud)在window.h
class Window
{ …Run Code Online (Sandbox Code Playgroud)我不知道如何用它来表达它或它实际上被称为什么,但我知道在Objective-C中你可以有多个构造函数可以连续相互调用,原谅任何代码错误,我有一段时间没有这样做,但这个想法就在那里.
- (id)initWithTitle:(NSString *)_title;
- (id)initWithTitle:(NSString *)_title page:(NSString *)_page;
-----------------------------------
- (id)initWithTitle:(NSString *)_title {
return [self initWithTitle:_title page:nil];
}
- (id)initWithTitle:(NSString *)_title page:(NSString *)_page {
if(self = [super init]) {
self.title = _title;
self.page = _page;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道在c ++中是否有相同的东西;
在Java中,我们可以编写如下:
public class Demo{
private int val;
public Demo(int val){this.val = val;}
public Demo(){this(0);}
}
Run Code Online (Sandbox Code Playgroud)
但是,它在C++中不起作用:
class Demo{
private:
int _val;
public:
Demo(int val):_val(val){}
Demo(){this(0);}
}
Run Code Online (Sandbox Code Playgroud)
如何修改此代码?
我正在使用SFML,并且具有类似以下的类:
#include <SFML\Graphics.hpp>
class Overlay
{
public:
Overlay(int width, int height);
Overlay(const sf::Texture*);
Overlay(const sf::Sprite*);
~Overlay();
private:
sf::Texture _texture;
sf::Image _img;
};
Run Code Online (Sandbox Code Playgroud)
现在,以下构造函数Overlay(const sf::Sprite*)正在运行:
Overlay::Overlay(const sf::Sprite* spr) {
int width = spr->getTexture()->getSize().x;
int height = spr->getTexture()->getSize().y;
_texture.create(width, height);
_img.create(width, height, sf::Color::Transparent);
}
Run Code Online (Sandbox Code Playgroud)
但是,以下嵌套的构造函数不是:
Overlay::Overlay(int width, int height)
{
_texture.create(width, height);
_img.create(width, height, sf::Color::Transparent);
}
Overlay::Overlay(const sf::Texture* tex) {
sf::Vector2u textureSize = tex->getSize();
Overlay(textureSize.x, textureSize.y);
}
Overlay::Overlay(const sf::Sprite* spr) {
Overlay(spr->getTexture());
}
Run Code Online (Sandbox Code Playgroud)
在我看来,如果执行以下命令,这两个代码片段应该做同样的事情:
sf::Sprite image;
Overlay mOverlay(&image);
Run Code Online (Sandbox Code Playgroud)
尽管它们都可以很好地编译,但是当调用第二个代码片段(嵌套构造函数)_img …
这是一个虚拟示例来说明:
class C
{
// complex class with many fields and methods
// including very expensive:
int computeA();
int computeB();
}
struct S
{
S(int a, int b); // initializes as {a, b, a*b};
// how to define below constructor?
S(const C& c); // Should be equivalent to calling S(c.computeA(), c.computeB())
int a;
int b;
int ab;
}
Run Code Online (Sandbox Code Playgroud)
我可能错过了一些简单的东西,但我所有的尝试在语法上都是不正确的。显然,我可以通过使用辅助函数而不是构造函数来规避这个问题,但是有没有正确的方法来直接定义这样的构造函数?
当然我不想这样做:
S(const C& c); // initialize as {c.computeA(), c.computeB(), c.computeA()*c.computeB()};
Run Code Online (Sandbox Code Playgroud)
因为重复不必要的昂贵计算。
我正在尝试创建一个Window类,但由于某种原因,一个Window对象的简单定义之后立即调用它的析构函数.
Window类的标题具有以下构造函数和定义的复制控件: -
Window();
Window(int);
Window(const char* title);
Window(string title);
Window(const char* title, int x, int y, int width, int height);
Window(string title, int x, int y, int width, int height);
Window(const Window &window);
Window& operator=(const Window &window);
~Window();
Run Code Online (Sandbox Code Playgroud)
这些功能的相关代码如下: -
Window::Window()
{
Window(default_title, default_width, default_height, default_xpos, default_ypos);
}
Window::Window(int)
:title_(default_title),
size_({ default_width, default_height }),
position_({ default_xpos, default_ypos })
{
context_ = glutCreateWindow(title_.c_str());
setposition(position_);
setsize(size_);
glutSetWindow(context_);
}
Window::Window(string title)
:Window(title, default_width, default_height, default_xpos, default_ypos)
{ }
Window::Window(const char* title)
{ …Run Code Online (Sandbox Code Playgroud)