Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Init Const Vars in a class 1

Status
Not open for further replies.

BorlandGeek

Programmer
Jun 14, 2004
27
CA
Does anyone know how you could init const vars in a class
I tried this

IN CLASS DECLERATION
const int var1;

IN CONSTRUCTOR
const_cast<int>(var1) = 10;

but this says it cannot modify const var
 
You have to initialize const attributes in the constructor's initialization member list.

An example:
Code:
class Dummy
{
  Dummy () :
    _pi (3.14159)
  {
  }

private
  const double _pi;
};

--
Globos
 
Sorry, the example corrected:

Code:
class Dummy
{
public:
  Dummy () :
    _pi (3.14159)
  {
  }

private:
  const double _pi;
};

--
Globos
 
Thanks ill try that... thanks for the
Class "Dummy" part too well deserved
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top