2015-11-29 54 views
0

我的程序基本上依賴於setter來初始化我的對象實例中的數據,但我想刪除它們並使用構造函數來替換setters,是否有辦法實現這或可以有人提供給我一個參考?我該如何使用構造函數來代替setter成員函數

實例化對象

//Using SDL and standard IO 
#include <SDL.h> 
#include <stdio.h> 
#include <iostream> 
#include <string> 
#include <iomanip> 
#include <archer.hpp> 
#include <ctime> 
#include <ArmouredArcher.hpp> 
#include <RNGI.hpp> 

    using namespace std; //Declaring use of namespace std 

    void instantiateMuskateer(); 

int main(int argc, char* argv[]) 
{ 
    //init muskateer object 
    instantiateMuskateer(); 
    system("pause"); 
    return 0; 
} 

實例化,活動和破壞

void instantiateMuskateer() 
{ 
    Archer* Muskateer = new Archer(); 

    Muskateer->setName("Brett"); 

    delete Muskateer; 
} 

.HPP文件

#ifndef _Archer_ 
#define _Archer_ 

#include <string> 

class Archer 
{ 
public: 
    inline Archer() : 
     name(""), 
     healthpoints(0), 
     baseDamage(0), 
     range(0) 
     { ; } //All Member varials are in a known state 

    inline Archer(std::string name, int healthpoints, int baseDamage, int range) : 
     name(name), 
     healthpoints(healthpoints), 
     baseDamage(baseDamage), 
     range(range) //All member variables are in a known state 
    { 
     ; 
    } 

    inline ~Archer() { ; } // empty destructor 

    inline std::string getName() { return name; } 
    inline void setName(std::string name) { this->name = name; } 

    inline int getHealthPoints() { return healthpoints; } 
    inline void setHealthPoints(int healthpoints) { this->healthpoints = healthpoints; } 

    inline int getBaseDamage() { return baseDamage; } 
    inline void setBaseDamage(int baseDamage) { this->baseDamage = baseDamage; } 

    inline int getRange() { return range; } 
    inline void setRange(int range) { this->range = range; } 

    /*std::string getName(); //getter for name 
    void setName(std::string name); //Set the name 

    int getHealthPoints(); 
    void setHealthPoints(int healthpoints); 

    int getBaseDamage(); 
    void setBaseDamage(int baseDamage); 

    int getRange(); 
    void setRange(int range); */ 
protected: 
private: 
    // copy constructor 
    Archer(const Archer& other) = delete; 
    // overload assignment operator 
    Archer& operator=(const Archer& other) = delete; 

    std::string name; 
    int healthpoints; 
    int baseDamage; 
    int range; 
}; 

#endif 
+0

該代碼的實際錯誤是什麼?乍一看對我來說很好。 –

+0

您可能正在尋找[Builder](https://en.wikipedia.org/wiki/Builder_pattern)模式。 –

+0

@πάνταῥεῖ代碼是正確的,但我想用合適的構造函數代替setter,但我不知道如何:( –

回答

1

在您的例子,它是非常簡單的,你只要把你在你的構造函數需要的參數:

Archer(std::string n) : 
     name(n), 
     healthpoints(0), 
     baseDamage(0), 
     range(0) 
     {} //All Member varials are in a known state 

然後你就可以簡單地這樣做:

void instantiateMuskateer() 
{ 
    Archer* Muskateer = new Archer("Brett"); 

    delete Muskateer; 
} 

幾點意見不相關,但要改善你的代碼。當你在你的類中聲明並實現你的函數時,編寫inline是無用的,暗示了inline。另外,如果你的析構函數什麼也不做,你不應該定義它或者使用= default,這樣你可以從編譯器啓用一些優化。另外,在你之前的函數中,我看不到需要在堆上分配對象,它又是性能的損失和錯誤的來源(例如忘記對象的delete),將它分配到堆棧上:

void instantiateMuskateer() 
{ 
    Archer Muskateer("Brett"); 
    // do your things 
} 

或使用unique_ptr

相關問題