472,345 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

"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 1955
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(DebugOptions());

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(DebugOptions());

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
+
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
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
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...
4
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
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...
9
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: ...
5
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...
3
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...
14
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.