473,766 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialize a const in class

nik
hello friends,
a class having two const variables ,how can we initialize these
variables

Jul 24 '06 #1
6 19175
"nik" <am*******@gmai l.comschrieb im Newsbeitrag
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
hello friends,
a class having two const variables ,how can we initialize these
variables
First of all, there are no const variables. A variable is something that may
change, that is variable. A constant is something that will not change (once
it has been initialiazied). So you probably mean a constant data member.

A constant data member can only be initialized in the initializer list of a
constructor:

class SomeClass
{
const int cannotBeModifie d;
public:
SomeClass(int initilialValue) : cannotBeModifie d(initialValue)
{}
};
HTH
Heinz

Jul 24 '06 #2
nik wrote:
hello friends,
a class having two const variables ,how can we initialize these
variables
In an initialiser list in the constrictor

struct X
{
const int n;

X() : n(42) {}
};

--
Ian Collins.
Jul 24 '06 #3

nik писал(а):
hello friends,
a class having two const variables ,how can we initialize these
variables
and if static:

class X
{
static const int hello;
};

X::hello = 111;

Jul 24 '06 #4

nik писал(а):
hello friends,
a class having two const variables ,how can we initialize these
variables
if static:

class X
{
static const int hello;
};

X::hello = 111;

Jul 24 '06 #5
bachelor wrote:
nik ?????(?):
>hello friends,
a class having two const variables ,how can we initialize these
variables

if static:

class X
{
static const int hello;
};

X::hello = 111;
Posting wrong answer twice doesn't make it right.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '06 #6
Victor Bazarov wrote:
bachelor wrote:
>nik ?????(?):
>>hello friends,
a class having two const variables ,how can we initialize these
variables
if static:

class X
{
static const int hello;
};

X::hello = 111;

Posting wrong answer twice doesn't make it right.
Why don't you give the correct answer, then.
>
V
Well, forget about it... Here is the correct answer:

[*************** ************ HEADER FILE *************** *************]
class MyClass{
public:
...
private:
...
const static int _somevar;
};

[*************** ************* C++ FILE *************** *************** **]
....
int MyClass::_somev ar = somevalue;
Aug 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2719
by: Jaakko Holster | last post by:
Lets run following code: ------- snip ------ class Perm { static $read = bindec('001'); static $write = bindec('010'); static $delete = bindec('100'); }
3
17639
by: Jan13 | last post by:
Hi, I'm new to programming in C++ (using VC6) and ran into the following problem: I want to declare and define a class member variable as 'static const', but something seems to go wrong with the linking.
2
550
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual DataType GetType() { return DataType( "Object" ); } protected: const static DataType _dataType( "Object");
3
3614
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
3
2238
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
7
2767
by: Nan Li | last post by:
Hello, I got stuck in a situation I think many of you must have already experienced. I want to have a simple lookup map inside my class. All the instances share that map. The code is as follows. class A { private: static std::map<int,std::string> someMap; public:
4
1921
by: dvir | last post by:
Hi I have a runing error while trying to initialize a class. this is my code: main.C ------------- #include "tdagent.H" #include "simple_env.H" #include <vector>
2
7250
by: Angus | last post by:
Hello I have some classes defined in shapes.h and use them like this: const IShape* rectangle = new Rectangle(10.0, 20.0); // width, height cout << rectangle->calculateArea() << endl; delete rectangle; // causes error C2665 error
2
1730
by: dolphin | last post by:
Why should we use fun(const Class &B) instead of fun(Class &B)?
2
9589
by: Kiran Pagar | last post by:
For an API of software named Teamcenter I need to pass arguments to function in following format. function(const char **str1, const char ** str2) So, I have to initialize const char **str.
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.