473,396 Members | 2,068 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,396 software developers and data experts.

const and folding

Hi,

In a header file I tried const char *someval="this is a test"; which
is included all over the place and I get linker errors about multiple
defines.

Why is this not folded when const char someval[]="this is a test"; is
and I get no linker errors?


--

Adrian

Think you know a language? Post to comp.lang... and find out!
Dec 7 '06 #1
2 2215
A message was posted about 5 minutes ago about global variables in
header files.
See
http://groups.google.com/group/comp....bf9f0e7fa85fdf

Joseph.

Dec 7 '06 #2

Adrian wrote:
Hi,

In a header file I tried const char *someval="this is a test"; which
is included all over the place and I get linker errors about multiple
defines.

Why is this not folded when const char someval[]="this is a test"; is
and I get no linker errors?
Note that someval is not terminated and the compiler will ignore the
"..." if it finds someval[] to have already been defined. You'll
probably get a warning if you turn on some compiler options.

Headers should not hold definitions in its declarations. Headers should
also use include guards so as to prevent mutiple inclusions - maybe you
did, nobody else knows. Consider using std::string instead.
Perhaps you might use the following as a guide for const char* :
a) as a member
b) as a static str
c) as an external global str

____________
// A.h
#ifndef A_H_
#define A_H_ // <- include guard

class A {
static const char* p_stat; // static str
const char* p; // member str
public:
A(const char* p_);
/* member functions */
const char* const get_str() const;
static const char* const get_static_str();
};

#endif // include guard A_H_

____________
// A.cpp
#include "A.h"

const char* g_str = "global string";
const char* A::p_stat = "static string";

A::A(const char* p_) : p(p_)
{
}

const char* const A::get_str() const
{
return p;
}

const char* const A::get_static_str()
{
return p_stat;
}

____________
// test.cpp
#include <iostream>
#include <ostream>
#include "A.h"

extern const char* g_str;

int main()
{
A a("member string");
std::cout << a.get_str() << std::endl;
std::cout << a.get_static_str() << std::endl;
std::cout << g_str << std::endl;
}

/*
member string
static string
global string
*/

Dec 7 '06 #3

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

Similar topics

1
by: Nuff Said | last post by:
Question: Is there a way to unfold a Python class *without* unfolding all its methods etc. so that you can get a quick overview of the class? Details: I normally open a file either with...
0
by: Jorge Godoy | last post by:
Hi! Does anyone here have a recipe or elisp code to fold classes or methods in XEmacs while using python mode? The standard folding mode didn't worked out of the box here... -- Godoy. ...
0
by: Terry Hancock | last post by:
My general attitude towards IDEs and editors has been extremely conservative, but today I decided to see what this "folding" business was all about. I see that vim (and gvim, which is what I...
0
by: Steve - DND | last post by:
I tried asking this in the VSNet.General newsgroup, but got no response; so I figured I would ask here. Is there any way that I can fold my HTML code in VS.Net for ASP.Net pages? This is a...
14
by: John Salerno | last post by:
Specifically, I'm using UltraEdit and perhaps there's no way perfect way to implement code folding with it, given how it uses its syntax highlighting file to do so (i.e., you have to specify an...
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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 project—planning, coding, testing,...

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.