473,440 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

const char as class member

Hi,

We can do:

class CSomething {
enum {
ITEM0 = 10,
ITEM1 = 20,
ITEM2 = 30
};
};

but can we do something similar with a constant string?

class CSomething {
const char *pString = "APPLE";
};

I want to be able to do something like:

int main()
{
printf("The static string is [%s]", CSomething::pString);

return 0;
}
Thanks

Jul 20 '06 #1
3 6163
markww wrote:
We can do:

class CSomething {
enum {
ITEM0 = 10,
ITEM1 = 20,
ITEM2 = 30
};
};

but can we do something similar with a constant string?

class CSomething {
const char *pString = "APPLE";
};
No.
I want to be able to do something like:

int main()
{
printf("The static string is [%s]", CSomething::pString);

return 0;
}
C strings need to be initialised where they are _defined_.

Also, don't forget the proper access specifier. As written, your
program won't compile also due to 'pString's being *private*.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #2
markww wrote:
Hi,

We can do:

class CSomething {
enum {
ITEM0 = 10,
ITEM1 = 20,
ITEM2 = 30
};
};

but can we do something similar with a constant string?

class CSomething {
const char *pString = "APPLE";
You probably want "const char* const" and you'll need to add to that
"static" and to define it in the cpp file, not the header.

// In A.hpp
struct A
{
static const char* const pString;
};

// In A.cpp
const char* const pString = "apple";
};

I want to be able to do something like:

int main()
{
printf("The static string is [%s]", CSomething::pString);
Prefer iostreams. Printf is not typesafe.
>
return 0;
}
Thanks
Cheers! --M

Jul 20 '06 #3
markww posted:

but can we do something similar with a constant string?

class CSomething {
const char *pString = "APPLE";
};

Either:

/* example1.hpp */

class Arb {
char const static *const str;
};
/* example1.cpp */

char const *const Arb::str = "Apple";
Or:

/* example2.hpp */

class Arb {
char const static str[];
};
/* example2.cpp */

char const Arb::str[] = "Apple";


--

Frederick Gotham
Jul 20 '06 #4

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

Similar topics

2
by: zoe | last post by:
Is it possible to initialize a non-static const array class-member of a fundamental type? Something like: class Foo{}{ public: Foo(); private: const int myArray;
9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
2
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
3
by: Peter | last post by:
Hi everybody, I am unfortunately stuck with a probably very simple problem. I made a class called Particle with a valarray (STL) as a class member. The code for the class goes like this: ...
2
by: Bjoern Knafla | last post by:
Hi! I am trying to specialize a member function of a template class (see code below). The moment the header with the "template class definition" and "template class member function...
3
by: Henryk | last post by:
I'm using the gcc with the -pedantic setting. On my embedded system I need a parameter class that contains all setting for the software. This class needs to be ROM-able to locate it in some kind...
3
by: zaeminkr | last post by:
I have class member data which is a pointer to class member function. However, I'm fail to compile the code. What is the correct grammar to make a function call by using static member data...
3
by: Alexander Hans | last post by:
Hi, what's the usual way in C++ to define a static constant class member? Consider the following example: class TestClass { public: static const double a = 12.5; static const double b = 2...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.