如何使用构造函数在另一个类中创建一个对象? [英] How to create an object inside another class with a constructor?

查看:288
本文介绍了如何使用构造函数在另一个类中创建一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写我的代码,它是以模块化的方式设计的。现在,我的一个班;调用 Splash 必须创建另一个类的对象,名为 Emitter 。通常,你只需创建对象,并使用它,但这不工作在这里,因为 Emitter 类有一个自定义构造函数。

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.

例如:

我试图这样做, t work:

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")
       {};
}

编辑:我知道第二种方式工作,然而它不。如果我删除 Emitter 节,代码工作。但是当我以第二种方式,没有窗口打开,没有应用程序被执行。

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.

那么如何创建我的发射器

编辑

这是我的代码为发射器类和头:

Here is my code for the emitter class and 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

这里是发射器功能:

#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();
    }
}

这里还有 Splash类 Splash标题

推荐答案

第二个选项应该工作,我会开始看编译错误,看看为什么没有。事实上,请发布任何与此代码相关的编译错误。

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; }

};

这篇关于如何使用构造函数在另一个类中创建一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆