C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 20th, 2006, 09:48 AM
Canasta Canasta is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 108 Canasta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 19 h 30 m 30 sec
Reputation Power: 9
Const char as class member

Hi,

We can do this:
Code:
class CSomething {
    public:
        enum {
            SOME_DEFINE = 10,
            NEXT_DEFINE
        };
}

Can we have string constants though in the same way?
Code:
class CSomething {
    public:
        const char *STRING_DEFINE = "some constant code";
}

I just want to call something like:
Code:
main ()
{
    printf ("the code is: %s", CSomething::STRING_DEFINE);
}

Thanks

Reply With Quote
  #2  
Old July 20th, 2006, 03:23 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Based on your code, you might want a static const char * member:

Code:
class testclass {
public:
    static const char *teststring;
};

const char *testclass::teststring = "hello";

int main(int argc, char **argv) {
    cout << testclass::teststring << endl;
}

Reply With Quote
  #3  
Old July 20th, 2006, 08:26 PM
280Z28 280Z28 is offline
Turning coffee into code
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: UT Austin, TX
Posts: 164 280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level)280Z28 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 20 h 37 m
Reputation Power: 93
Send a message via AIM to 280Z28
If you really don't want the string changing, it needs to be:

Code:
class testclass {
public:
    static const char* const teststring;
};

const char* const testclass::teststring = "hello";

int main(int argc, char **argv) {
    cout << testclass::teststring << endl;
}
Comments on this post
peenie agrees!
Lux Perpetua agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Const char as class member

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap