473,772 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"static" constructor parameter confused with object name?

Consider this code snippet which doesn't compile:

struct DebugOptions {
};

class Debug {
public:
Debug(const DebugOptions options) { _options = options; }

private:
static DebugOptions _options;
};

DebugOptions Debug::_options ;

main() {
DebugOptions d;
Debug::Debug(d) ;
}

jturian@bellini :/tmp 1$ gcc -o d d.cc
d.cc: In function 'int main()':
d.cc:16: error: conflicting declaration 'Debug d'
d.cc:15: error: 'd' has a previous declaration as 'DebugOptions d'

How can I call Debug::Debug using d as a parameter?

Thanks,

Joseph

Sep 20 '06 #1
9 2070
Joseph Turian wrote:
Consider this code snippet which doesn't compile:

struct DebugOptions {
};

class Debug {
public:
Debug(const DebugOptions options) { _options = options; }

private:
static DebugOptions _options;
};

DebugOptions Debug::_options ;

main() {
main returns int. _Always_
DebugOptions d;
Debug::Debug(d) ;
You can't call a constructor directly. You can make this compile by
changing the above line to:

Debug temp(d);

But this is a strange way of doing this - why are you using a
constructor to set a static member variable anyway? Why not just use a
static function, or better yet, just use a namespace and get rid of the
class entirely. Are you a java programmer?

Best regards,

Tom

Sep 20 '06 #2
Thomas Tutone wrote:
Joseph Turian wrote:
Consider this code snippet which doesn't compile:

struct DebugOptions {
};

class Debug {
public:
Debug(const DebugOptions options) { _options = options; }

private:
static DebugOptions _options;
};

DebugOptions Debug::_options ;

main() {

main returns int. _Always_
DebugOptions d;
Debug::Debug(d) ;

You can't call a constructor directly. You can make this compile by
changing the above line to:

Debug temp(d);

But this is a strange way of doing this - why are you using a
constructor to set a static member variable anyway? Why not just use a
static function, or better yet, just use a namespace and get rid of the
class entirely. Are you a java programmer?

Best regards,

Tom
Hi Joseph,

Tom already pointed out most of what is wrong. No offense meant, but
your code looks horrible, clumsy, inexperienced, hence the Java
question ;-) (most Java programmers tend to be conditioned into writing
code in a somewhat object-fuscated style) Apart from that: don't use
_options. Qualifiers with leading _ are reserved for the compiler +
implementation.

A quick implementation in the C++ way to do things would rather look
like this (You don't really need the struct, but it can bew helpful if
you want to organize different classes of debug options.):

--- file debug.h
namespace debug {
struct Options {
int a;
};
extern Options options;
}

--- file debug.cpp
#include "debug.h"
namespace debug {
Options options;
}

--- file main.cpp
#include "debug.h"
int main () {
debug::options. a = 42;
// Do something
}

Sep 20 '06 #3
Joseph Turian posted:
Consider this code snippet which doesn't compile:

struct DebugOptions {
};

class Debug {
public:
Debug(const DebugOptions options) { _options = options; }

private:
static DebugOptions _options;
};

DebugOptions Debug::_options ;

main() {

Implicit int was a feature of C89. It disappeared with C99 and also with
C++.

DebugOptions d;
Debug::Debug(d) ;

I don't know where you're getting that from -- why haven't you written the
following:

Debug object_name(d);

, or if your intent was to create a nameless object:

Debug(d);

--

Frederick Gotham
Sep 20 '06 #4
Frederick Gotham wrote:
Joseph Turian posted:
struct DebugOptions {
};

class Debug {
public:
Debug(const DebugOptions options) { _options = options; }

private:
static DebugOptions _options;
};

DebugOptions Debug::_options ;

main() {
[snip]
DebugOptions d;
Debug::Debug(d) ;


I don't know where you're getting that from -- why haven't you written the
following:
[snip]
, or if your intent was to create a nameless object:

Debug(d);
Fred, did you try compiling that? I think you mean:

Debug(DebugOpti ons());

Best regards,

Tom

Sep 20 '06 #5
Thomas Tutone posted:
>, or if your intent was to create a nameless object:

Debug(d);

Fred, did you try compiling that? I think you mean:

Debug(DebugOpti ons());

I should have written:

(Debug)d;

, as the intent is to create a nameless temporary and pass "d" as the
argument to its constructor.

Yet another bastard inconsistency between C and C++, all steming from "If it
looks like a declaration, it's a declaration".

--

Frederick Gotham
Sep 20 '06 #6

F.J.K. wrote:
Apart from that: don't use
_options. Qualifiers with leading _ are reserved for the compiler +
implementation.
Could you please explain?
What do you mean by qualifier, precisely?
Using underscore anywhere, even member variables, is deprecated?

Thanks,

Joseph

Sep 21 '06 #7

F.J.K. wrote:
Tom already pointed out most of what is wrong. No offense meant, but
your code looks horrible, clumsy, inexperienced, hence the Java
question ;-) (most Java programmers tend to be conditioned into writing
code in a somewhat object-fuscated style)
Please suggest a redesign.

I currently have a class Debug. It is used globally to handle Debug
output (output it to a tee'd stream), and its behavior is controlled by
DebugOptions, which are passed in the constructor. So, it is typical to
use Debug static methods:
class Debug {
static ostream& log(unsigned debuglevel);
...
};
Debug::log(3) << "Now I'm logging at to the debug log at debug level
3.\n";

I don't know if namespaces will work. The reason I'm using an object is
because I want certain things output when the program terminates, and I
do this by defining a destructor for Debug.

Any suggestions about a less horrible, clumsy, and/or inexperienced
approach are welcome.

Thanks,

Joseph

Sep 21 '06 #8

Joseph Turian wrote:
F.J.K. wrote:
Apart from that: don't use
_options. Qualifiers with leading _ are reserved for the compiler +
implementation.

Could you please explain?
What do you mean by qualifier, precisely?
Using underscore anywhere, even member variables, is deprecated?
17.4.3.1/3
If the program declares or defines a name in a context where it is
reserved, other than as explicitly allowed by this clause, the behavior
is undefined.

17.4.3.1.2/1
Certain sets of names and function signatures are always reserved to
the implementation:
Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter is reserved to the
implementation for any use.
Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.

So using a name (whether it be the name of a variable, function, type,
namespace or anything else) yields undefined behaviour, which is a
completely different concept to being deprecated.

In your code you had a member variable called _options. As a member
variable, it is not in the global namespace. The name exists only
within the scope of the class. So the second part of 17.4.3.1.2/1 does
not apply. And nothing about the first part of 17.4.3.1.2/1 applies
either. So in this case F.J.K. was wrong and your code is fine.
However, rather than having to remember exactly which rules apply where
(I had to look it up again to check before I worte this post) many
people, myself included, prefer simply to avoid leading underscores
altogether. Note that, for example, if your member variable had been
called _Options, the first part of 17.4.3.1.2/1 would apply and you
would not be allowed to use that name.

Gavin Deane

Sep 21 '06 #9
Gavin Deane wrote:
Joseph Turian wrote:
>F.J.K. wrote:
>>Apart from that: don't use
_options. Qualifiers with leading _ are reserved for the compiler
+
implementatio n.

Could you please explain?
What do you mean by qualifier, precisely?
Using underscore anywhere, even member variables, is deprecated?

17.4.3.1/3
If the program declares or defines a name in a context where it is
reserved, other than as explicitly allowed by this clause, the
behavior is undefined.

17.4.3.1.2/1
Certain sets of names and function signatures are always reserved to
the implementation:
Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter is reserved to the
implementation for any use.
Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.

So using a name (whether it be the name of a variable, function,
type,
namespace or anything else) yields undefined behaviour, which is a
completely different concept to being deprecated.

In your code you had a member variable called _options. As a member
variable, it is not in the global namespace. The name exists only
within the scope of the class. So the second part of 17.4.3.1.2/1
does
not apply. And nothing about the first part of 17.4.3.1.2/1 applies
either. So in this case F.J.K. was wrong and your code is fine.
However, rather than having to remember exactly which rules apply
where (I had to look it up again to check before I worte this post)
many people, myself included, prefer simply to avoid leading
underscores altogether. Note that, for example, if your member
variable had been called _Options, the first part of 17.4.3.1.2/1
would apply and you would not be allowed to use that name.

Gavin Deane
Also, using _option as the name for a member variable is not very
useful, as we will then know that it is *either* a local variable, or
a global implementation specific name. That makes it a pretty useless
convention, even though it is legal.
Bo Persson
Sep 21 '06 #10

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

Similar topics

29
4413
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
4
1540
by: Pat | last post by:
I would like to know what is the meaning of : static vector<int> v; What is the difference with: vector<int> v;
12
13471
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one of your Derived classes will need to implement some method, but implement it differently, and that the base class cannot implement it. This is where pure virtual comes in.
9
2310
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
5
8865
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
3
5859
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ section describes a solution using methods returning references to static objects. But consider:
14
6027
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9621
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
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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...
1
10039
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
8937
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...
0
6716
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();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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
2851
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.