PDA

View Full Version : C++ readonly property



yyiu002
20th June 2010, 21:58
Hi, I want to implement a read only property for a class, can anyone show me how to do this?

Currently I am using a function, is this the correct way? Can anyone explain what is this "const" means for function return? I just put there, it compiles, but have no clue what it will do? I guess it will stop user from changing the return value?
Class A
{
private:
QString myID;

public QString ID() const {return myID;}
}

SixDegrees
20th June 2010, 22:05
1) make the data you want to protect protected, or private.

2) create accessor functions that allow you to read the data, but not write it or change it in any way.

yyiu002
20th June 2010, 22:14
I just updated showed my code, is it the correct way?

SixDegrees
20th June 2010, 22:24
Yes, that will work.

Zlatomir
20th June 2010, 22:34
Yes, it will indeed work, but now you have a class A, with a empty QString as a member, and a function that allways returns an empty QString... this doesn't sound very usefull :confused:

So i guess you will need the constructor to initialize the QString member, or another method to create a usefull string in there.

squidge
20th June 2010, 23:02
const functions just means that the function will not modify any class variables.

yyiu002
20th June 2010, 23:20
const functions just means that the function will not modify any class variables.

What is the difference between following 2 lines?

Thanks.

public QString ID() const {return myID;}

public const QString ID() {return myID;}

yyiu002
20th June 2010, 23:29
I did this in my constructor: A::A(QString comPort, QString baudRate):myID(comPort+":"+baudRate)

But still a worry that myID might not be unique, I have create a list, each time adding an instance to check my ID whether is unique? Is there a better way to do this?

At C#, I might have a static constructor, generate my own ID each time when class get instantialized.

At C#:
Class A{

int myID;

static A(){
myID++;
}
}

How can I do similar thing in C++?

Zlatomir
21st June 2010, 00:43
public: QString ID() const {return myID;} // this is a function declared const fatjuicymole allready explained what this is

public: const QString ID() {return myID;} // this is a non-const function that returns a const QString

In c++ you can do something like:


class A{
static int myID;
public:
A(){
myID++;
}
//...
};

and outside of class you will need to initialize the static variable, like this:


int A::myID = 0;

yyiu002
21st June 2010, 01:27
public: QString ID() const {return myID;} // this is a function declared const fatjuicymole allready explained what this is

public: const QString ID() {return myID;} // this is a non-const function that returns a const QString

In c++ you can do something like:


class A{
static int myID;
public:
A(){
myID++;
}
//...
};

and outside of class you will need to initialize the static variable, like this:


int A::myID = 0;


Is this empty constructor gets called every time when I initialize it with a different constructor with parameters like following:

static int myID;
public:
A(QString myName){
}
//...
};

Thank you for your reply.

Zlatomir
21st June 2010, 01:33
No, only one constructor it's called (for each created object)

So you will need to increase that variable in each of them (and maybe you will want to define a copy constructor to increase the ID in case someone create a object by making a copy of one that already exist, because the default generated copy constructor won't increase that variable)

yyiu002
21st June 2010, 02:15
No, only one constructor it's called (for each created object)

So you will need to increase that variable in each of them (and maybe you will want to define a copy constructor to increase the ID in case someone create a object by making a copy of one that already exist, because the default generated copy constructor won't increase that variable)

Sorry I am little dummy on copy constructor? Please show me some code on how to do this.
Many thanks.

yyiu002
21st June 2010, 02:26
with line : int A::myID = 0;
When I put it inside header file where class A was defiined, I get error like: multiple definition of `A::myID', why is that? I am sure there are "#ifndef A_H
#define A_H " to stop class A was included more than once.

hackerNovitiate
21st June 2010, 07:09
with line : int A::myID = 0;
When I put it inside header file where class A was defiined, I get error like: multiple definition of `A::myID', why is that? I am sure there are "#ifndef A_H
#define A_H " to stop class A was included more than once.

The #ifndef makes sure that each .cpp file only includes one copy of the header. But if you have 5 .cpp files that have "#include a.h", then the header will be included in 5 files, so A::myID will be defined 5 times. To fix that, move "int A::myID = 0" from the header into one of your .cpp files (but put it outside a function, just like how you define a global variable).

Also, here is a comprehensive article that describes all the ways to use "const" in C++: http://duramecho.com/ComputerInformation/WhyHowCppConst.html

You only need a copy constructor if you want to make a copy of an existing object. If you do, have a look at http://171.64.64.250/class/cs193d/handouts/14CopyConstructors.pdf

Zlatomir
21st June 2010, 08:22
with line : int A::myID = 0;
When I put it inside header file where class A was defiined, I get error like: multiple definition of `A::myID', why is that? I am sure there are "#ifndef A_H
#define A_H " to stop class A was included more than once.

You got to learn about c++ way of doing this things, so you create a header file (a.h) for the class definition (here you don't write the implementation of the functions, just the definition) and you write a second file (a.cpp) that include the a.h and here you write the implementation (it's not like in c#) and don't forget about the include guards (they are not useless)
Example:


//a.h file
#ifndef A_H
#define A_H
class A{
static int myID;
int member;
public:
A();
A(A&); //copy c-tor
int id(); // function that returns the id
// rest of the class definition
//...
};
#endif // end of define A_H

and the cpp file:


//a.cpp
#include "a.h"

int A::myID = 0;

A::A(){
myID++;
}

A::A(A& ref){ //implementation of copy constructor
// here you copy the members from the ref object passed to the constructor
member = ref.member; //just an example here member is uninitialized... you need to take care of all the data members

myID++; // and increment the static variable...
}
int A::id() {
return myID;}

yyiu002
21st June 2010, 22:05
At article http://www.codersource.net/c/c-miscellaneous/c-copy-constructors.aspx
it says following

There are 3 important places where a copy constructor is called.

1. When an object is created from another object of the same type
2. When an object is passed by value as a parameter to a function
3. When an object is returned from a function

case 2 and 3 could be a problem.

Zlatomir
22nd June 2010, 10:26
case 2 and 3 could be a problem.
Why is case 2 and 3 problem?