C++ 如何使用构造函数在另一个类中创建对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11852645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an object inside another class with a constructor?
提问by Rivasa
So I was working on my code, which is designed in a modular way. Now, one of my classes; called Splash
has to create a object of another class which is called Emitter
. Normally you would just create the object and be done with it, but that doesn't work here, as the Emitter
class has a custom constructor. But when I try to create an object, it doesn't work.
所以我正在研究我的代码,它是以模块化方式设计的。现在,我的一堂课;被调用Splash
必须创建另一个被调用的类的对象Emitter
。通常,您只需创建对象并完成它,但这在这里不起作用,因为Emitter
该类具有自定义构造函数。但是当我尝试创建一个对象时,它不起作用。
As an example;
举个例子;
Emitter
has a constructor like so: Emitter::Emitter(int x, int y, int amount);
and needs to be created so it can be accessed in the Splash
class.
Emitter
有一个像这样的构造函数:Emitter::Emitter(int x, int y, int amount);
并且需要创建以便在Splash
类中可以访问它。
I tried to do this, but it didn't work:
我试图这样做,但没有奏效:
class Splash{
private:
Emitter ps(100, 200, 400, "firstimage.png", "secondimage.png"); // Try to create object, doesn't work.
public:
// Other splash class functions.
}
I also tried this, which didn't work either:
我也试过这个,这也不起作用:
class Splash{
private:
Emitter ps; // Try to create object, doesn't work.
public:
Splash() : ps(100, 200, 400, "firstimage.png", "secondimage.png")
{};
}
Edit:I know the second way is supposed to work, however it doesn't. If I remove the Emitter
Section, the code works. but when I do it the second way, no window opens, no application is executed.
编辑:我知道第二种方法应该有效,但事实并非如此。如果我删除该Emitter
部分,代码将起作用。但是当我用第二种方式做时,没有窗口打开,没有应用程序被执行。
So how can I create my Emitter
object for use in Splash
?
那么如何创建我的Emitter
对象以用于Splash
?
Edit:
编辑:
Here is my code for the emitter class and header:
Header
这是我的发射器类和标题的代码:
Header
// Particle engine for the project
#ifndef _PARTICLE_H_
#define _PARTICLE_H_
#include <vector>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "image.h"
extern SDL_Surface* gameScreen;
class Particle{
private: // Particle settings
int x, y;
int lifetime;
private: // Particle surface that shall be applied
SDL_Surface* particleScreen;
public: // Constructor and destructor
Particle(int xA, int yA, string particleSprite);
~Particle(){};
public: // Various functions
void show();
bool isDead();
};
class Emitter{
private: // Emitter settings
int x, y;
int xVel, yVel;
private: // The particles for a dot
vector<Particle> particles;
SDL_Surface* emitterScreen;
string particleImg;
public: // Constructor and destructor
Emitter(int amount, int x, int y, string particleImage, string emitterImage);
~Emitter();
public: // Helper functions
void move();
void show();
void showParticles();
};
#endif
and here is the emitter functions:
这是发射器功能:
#include "particle.h"
// The particle class stuff
Particle::Particle(int xA, int yA, string particleSprite){
// Draw the particle in a random location about the emitter within 25 pixels
x = xA - 5 + (rand() % 25);
y = yA - 5 + (rand() % 25);
lifetime = rand() % 6;
particleScreen = Image::loadImage(particleSprite);
}
void Particle::show(){
// Apply surface and age particle
Image::applySurface(x, y, particleScreen, gameScreen);
++lifetime;
}
bool Particle::isDead(){
if(lifetime > 11)
return true;
return false;
}
// The emitter class stuff
Emitter::Emitter(int amount, int x, int y, string particleImage, string emitterImage){
// Seed the time for random emitter
srand(SDL_GetTicks());
// Set up the variables and create the particles
x = y = xVel = yVel = 0;
particles.resize(amount, Particle(x, y, particleImage));
emitterScreen = Image::loadImage(emitterImage);
particleImg = particleImage;
}
Emitter::~Emitter(){
particles.clear();
}
void Emitter::move(){
}
void Emitter::show(){
// Show the dot image.
Image::applySurface(x, y, emitterScreen, gameScreen);
}
void Emitter::showParticles(){
// Go through all the particles
for(vector<Particle>::size_type i = 0; i != particles.size(); i++){
if(particles[i].isDead() == true){
particles.erase(particles.begin() + i);
particles.insert(particles.begin() + i, Particle(x, y, particleImg));
}
}
// And show all the particles
for(vector<Particle>::size_type i = 0; i != particles.size(); i++){
particles[i].show();
}
}
Also here is the Splash Classand the Splash Header.
回答by Jonathan Henson
The second option should work, and I would start looking at compilation errors to see why it doesn't. In fact, please post any compilation errors you have related to this code.
第二个选项应该有效,我将开始查看编译错误以了解为什么它不起作用。事实上,请发布与此代码相关的任何编译错误。
In the meantime, you can do something like this:
同时,您可以执行以下操作:
class Splash{
private:
Emitter* ps;
public:
Splash() { ps = new Emitter(100,200,400); }
Splash(const Splash& copy_from_me) { //you are now responsible for this }
Splash & operator= (const Splash & other) { //you are now responsible for this}
~Splash() { delete ps; }
};
回答by Rivasa
Well, I managed to fix it, in a hackish way though. What I did was create a default constructor, and move my normal Constructor code into a new function. Then I created the object and called the the new init function to set everything up.
好吧,我设法以一种骇人听闻的方式修复了它。我所做的是创建一个默认构造函数,并将我的普通构造函数代码移动到一个新函数中。然后我创建了对象并调用了新的 init 函数来设置一切。