473,756 Members | 6,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2254
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_s tr()
{
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_st r() << 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
3097
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 'fold.on.open=1' set in the user's options file resp. I use 'toggle all folds' from
0
1207
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. <godoy@ieee.org>
0
1319
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 actually use) has this feature, and it is fairly nice, but at present it's very manual --- and frankly it's hard for me to see the point if I have to manually mark folds every time I start up.
0
992
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 tremendously useful feature when writing my code-behinds, but then going to my HTML for a large page is hideous. I don't know much about VS.Net addins, but if this functionality isn't present inherently, is there a way to allow folding through an addin...
14
1956
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 "Opening" and "Closing" character in which to enfold code, such as braces). But my question is more general: is it possible to implement code folding with Python given that it has no real block delimiters? Or is this still a matter of which...
39
2788
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 read somewhere that it when you won't modify after its initialization... So when exactly do I use one or another? Is it *wrong* not use const when I should?
0
9454
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10028
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9868
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9836
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
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, and deployment—without 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
7242
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
6533
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();...
1
3804
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
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.