0

my program basically depends on setters to initialize the data in my object instances but I want to remove them and have constructors in place of the setters, Is there a way I can do this or can anybody provide me a reference?

Instantiate object

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

Instantiation, Activity and destruction

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

    Muskateer->setName("Brett"); 

    delete Muskateer;
}

.hpp file

#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 
5
  • What's actually wrong with that code? Looks fine for me at a first glance. Commented Nov 29, 2015 at 18:31
  • You may be looking for a Builder pattern. Commented Nov 29, 2015 at 18:32
  • @πάνταῥεῖ The code is correct but I want to use appropriate constructors in place of the setters but I have no idea how to :( Commented Nov 29, 2015 at 18:36
  • @imbrett You mean how to call the constructor? Just pass all the parameters that were declared in the constructor. Commented Nov 29, 2015 at 18:38
  • You shouldn't use inline when the function definition is inline anyway, it just clutters the screen. It's meant to be used when the body is out-of-line but you want it to be treated as if it were inline. Commented Nov 29, 2015 at 23:55

1 Answer 1

1

In your example, it is really simple, you just have to take the parameters you need in your constructor:

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

And then you can simply do that:

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

    delete Muskateer;
}

A few comments not related, but to improve your code. Writing inline is useless when you declare and implement your functions inside your class, the inline is implied. Also, if your destructor does nothing, you should not define it or use = default, that way you can enable some optimizations from the compiler.

Also, in your previous function i see no need to allocate the object on the heap, it is again a loss of performance and a source of error (such as forgetting to delete the object), allocate it on the stack:

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

Or use a unique_ptr.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.