2017-03-09 6 views
0

내 기본 클래스에서 내 서브 클래스로 sf :: Sprite 및 sf :: Texture를 상속하려고 할 때 문제가있는 것 같습니다. 스프라이트와 텍스처를 사본으로 보내려고 할 때 일종의 작동하지만, 물론 이미지를 얻지는 않습니다. 이 문제를 어떻게 해결할 수 있는지 알고 있습니까? 내 기본 클래스 :SFML에서 스프라이트와 텍스처로 상속을 시도하는 중

#ifndef OBJECTHOLDER_H 
#define OBJECTHOLDER_H 
#include <SFML\Graphics.hpp> 
using namespace std; 

class ObjectHolder : public sf::Drawable { 

private: 
    float windowHeight; 
    float windowWidth; 
    sf::Texture texture; 
    sf::Sprite sprite; 
public: 
    ObjectHolder(); 
    virtual ~ObjectHolder(); 
    float getWindowHeight() const; 
    float getWindowWidth() const; 
    const sf::Sprite & getSprite() const; 
    const sf::Texture & getTexture() const; 
}; 

#endif //OBJECTHOLDER_H 

#include "ObjectHolder.h" 

ObjectHolder::ObjectHolder() { 
    float windowHeight; 
    float windowWidth; 
} 

ObjectHolder::~ObjectHolder() { 
} 

float ObjectHolder::getWindowHeight() const { 
    return this->windowHeight; 
} 

float ObjectHolder::getWindowWidth() const { 
    return this->windowWidth; 
} 

const sf::Sprite & ObjectHolder::getSprite() const { 
    return this->sprite; 
} 

const sf::Texture & ObjectHolder::getTexture() const { 
    return this->texture; 
} 

내 서브 클래스 :

#ifndef PROJECTILE_H 
#define PROJECTILE_H 
#include "ObjectHolder.h" 

class Projectile : public ObjectHolder { 
public: 
    Projectile(); 
    virtual ~Projectile(); 
    void move(const sf::Vector2f& amount); 
    virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const; 
}; 

#endif //PROJECTILE_H 

#include "Projectile.h" 
#include <iostream> 

Projectile::Projectile() { 
    if (!this->getTexture().loadFromFile("../Resources/projectile.png")) { 
     cout << "Error! Projectile sprite could not be loaded!" << endl; 
    } 
    this->getSprite().setTexture(getTexture()); 
    this->getSprite().setPosition(sf::Vector2f(940.0f, 965.0f)); 
} 

Projectile::~Projectile() { 
} 

void Projectile::move(const sf::Vector2f & amount) { 
    this->getSprite().move(amount); 
} 

void Projectile::draw(sf::RenderTarget & target, sf::RenderStates states) const{ 
    target.draw(this->getSprite(), states); 
} 

답변

2

당신은 오히려 private보다 protected으로 해당 구성원을 표시 단지 수, 파생 클래스에 직접 액세스 할 수 있습니다 :

class Base { 
protected: 
    sf::Texture m_Texture; 
} 

class Derived : public Base { 
    Derived() { 
     m_Texture.loadFromFile("myTexture.png"); 
    } 
} 
+0

예 그것은 효과가 있었다. 고맙습니다! 나는 그렇게 쉽게 믿을 수 없다. 저는 앉아서 const와 호출 값을 매우 오랫동안 바꾸고 있습니다. 나는 옵션으로 보호받는 것을 생각하지 않았다. – Henke