Pass by const ref for arrays

Jan 24, 2012 at 2:06pm
I have the following class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class CharacterImpl
{
protected:
    int mStam, mWill, mRef, mInt, mStr, mPercp, mAgil, mAware, mVoid, mAir, mEarth, mWater, mFire;
    std::string mName;
    virtual int setRing(const int& x, const int& y) const;
    static int diceRoller(int numSkillDice, int numStatDice);
public:
    CharacterImpl(const std::string& inName, int inStats[]);
    virtual ~CharacterImpl();
    virtual void setStam(const int& inStam);
    virtual int getStam() const;
    virtual void setWill(const int& inWill);
    virtual int getWill() const;
    virtual void setRef(const int& inRef);
    virtual int getRef() const;
    virtual void setInt(const int& inInt);
    virtual int getInt() const;
    virtual void setStr(const int& inStr);
    virtual int getStr() const;
    virtual void setPercp(const int& inPercp);
    virtual int getPercp() const;
    virtual void setAgil(const int& inAgil);
    virtual int getAgil() const;
    virtual void setAware(const int& inAware);
    virtual int getAware() const;
    virtual void setVoid(const int& inVoid);
    virtual int getVoid() const;
    virtual int getAir() const;
    virtual int getEarth() const;
    virtual int getWater() const;
    virtual int getFire() const;
    virtual void setName(const std::string& inName);
    virtual std::string getName() const;
    virtual void printStats() const;
    virtual int rollEarth() const;
    virtual int rollAir() const;
    virtual int rollWater() const;
    virtual int rollFire() const;
};


I would like to be able to pass inStats[] to the constructor by const ref, like so:

CharacterImpl(const std::string& inName, const int& inStats[]);

This syntax doesn't work, however. Does anybody know how I can pass inStats as a const reference so that I can reduce overhead?
Jan 24, 2012 at 2:16pm
1
2
template<std::size_t N>
CharacterImpl(const std::string& inName, const int (&inStats)[N])

or just

CharacterImpl(const std::string& inName, const std::vector<int>& inStats)
Jan 24, 2012 at 2:20pm
I was actually using an array over a vector because the size of inStats is pre-determined and will never change, so I don't need the functionality of a vector. Is there another way to bass by const ref other than templating?
Jan 24, 2012 at 2:45pm
First note that in your code it is is passing the array as a pointer so there is no overhead. If you want pass the array by reference you can do as Cubbi has shown. If you don't want to use templates you can just write
CharacterImpl(const std::string& inName, const int (&inStats)[N])
and replace N with the size of the array.
Last edited on Jan 24, 2012 at 2:46pm
Jan 24, 2012 at 2:50pm
Passing an array is just the same as passing a pointer: your array will not copy if it is what you fear.

When you write CharacterImpl(const std::string& inName, const int& inStats[]); you are actually saying inStats is an array or reference which is impossible.

As an information, c++11 has introduced the new class std::array which is a fixed size array, you can use it as follow:
1
2
template<std::size_t N>
CharacterImpl(const std::string& inName, std::array<int, N> const& inStats)

It's just a more convinient way of doing like Cubbi said if you can use c++11.
Jan 24, 2012 at 3:05pm
@Peter87 & aquaz:

That's the information that I was looking for, thanks. Unfortunately, as I'm developing this in conjunction with a partner that has only the C++98 standard, I have to stay away from the snazzy new C++11 features, but I'll be sure to keep that syntax in mind for my future projects.
Topic archived. No new replies allowed.