LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-28-2008, 06:06 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
c++: can you set a string to a #defined value?


say I have:

Code:
#define foo BAR
is there a way to set a string to that? i.e.

Code:
string str = foo;
... that doens't work, of course, because foo is not a string and can't be seen as a char* being that it has no quotes. If I use "foo" then str is "foo "rather than "BAR".

Is this possible?
 
Old 02-28-2008, 06:08 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
maybe
Code:
string str = ##foo;
 
Old 02-28-2008, 06:10 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You need an extra two macros:
Code:
#define ACTUAL_STRINGIFY(str) #str
#define STRINGIFY(str) ACTUAL_STRINGIFY(str)

#define foo BAR
string str = STRINGIFY(foo);
You need two, otherwise it will make the string "foo" instead of "BAR".
ta0kira
 
Old 02-28-2008, 06:15 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
you guys... always so helpful.

Thanks!!
 
Old 02-28-2008, 06:28 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
You need an extra two macros:
Code:
#define ACTUAL_STRINGIFY(str) #str
#define STRINGIFY(str) ACTUAL_STRINGIFY(str)

#define foo BAR
string str = STRINGIFY(foo);
You need two, otherwise it will make the string "foo" instead of "BAR".
ta0kira
Yep ignore me, I even used the wrong operator.
 
Old 02-29-2008, 01:01 PM   #6
Wollongong
LQ Newbie
 
Registered: May 2007
Location: Australia
Distribution: Ubuntu 12.04LTS
Posts: 11

Rep: Reputation: 2
You may know this already, but "const" is preferred to "#define" when writing C++.
It works for all types including char * and for string:

#include <iostream>
#include <string>
using namespace std;

int main() {
const char * s1="new";
const string s2("old");
string s3=s2;
cout << "s3=" << s3 << ", label=" << s1 << endl;
}
 
Old 02-29-2008, 01:21 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Wollongong View Post
const char * s1="new";
This isn't a constant because you can change the pointer. You need to define it with [] instead of * for it to be a constant string, otherwise the string area may be overwritten by other assignments of string constants to const char*.
ta0kira
 
Old 02-29-2008, 01:54 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Wollongong View Post
You may know this already, but "const" is preferred to "#define" when writing C++.
It works for all types including char * and for string:
yes, but it doesn't work for some things where a #define will, like, for instance, a class name.

Long story, but I need a class to be called "foo" and I need to pass the string "foo" to a host application. I'd like to be able to change "foo" to "bar" without editing 8 different files in many different places. Sure, a script could do that for me, but so could a macro & would be somewhat more readable. This way I can do something like:

Code:
#define CLASSNAME foo

class CLASSNAME {
...
};

registerApp(STRINGIFY(CLASSNAME),&CLASSNAME::register, &CLASSNAME::initialize);
There are probably better ways of doing this, but... this one works quite well.
 
Old 02-29-2008, 04:25 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I do the same thing when defining built-in commands for a tool I'm writing. It works well because I can use it for function names, text comparisons, and error messages without having to remember to update 20 different occurrences of the same word.
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
How to set SNMP string man_linux Linux - Networking 1 11-07-2006 09:42 PM
where defined(__i386) is being defined?? syseeker Programming 1 06-27-2006 07:24 AM
What is a "string identifier" and how do I set it Ebisu_Dave Linux - Hardware 1 07-15-2005 06:11 PM


All times are GMT -5. The time now is 02:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration