473,394 Members | 1,944 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,394 software developers and data experts.

Is this right??Is redefine??

Because my english is very poor,so i give my program as follow:
////////////////////////////////////////////////////////////////////////////////////
one.hpp:
#include <iostream>
#ifndef TEST
#define TEST "The first print()"
inline void print()
{
std::cout<<TEST<<std::endl;
}

#else
#undef TEST
#define TEST "the second print()"
inline void print()
{
std::cout<<TEST<<std::endl;
}
#endif /*TEST*/
///////////////////////////////////////////////////////////////////////////////////////////////////
two.hpp:
#define TEST "none"
/////////////////////////////////////////////////////////////////////////////////////////////////
unit1.cpp:
#include "one.hpp"
#include "two.hpp" //attention about sequence of two headers
void doSomething()
{
print();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
unit2.cpp
#include "two.hpp"
#include "one.hpp" //attention about sequence of two headers
int main()
{
print();
}

what we can see there are two "print()"defined,and all in the globe
namespace and
why there is no error about redefine "print()"?

May 10 '06 #1
4 1732

redriver wrote:
/////////////////////////////////////////////////////////////////////////////////////////////////
unit1.cpp:
#include "one.hpp"
#include "two.hpp" //attention about sequence of two headers ///////////////////////////////////////////////////////////////////////////////////////////////////
unit2.cpp
#include "two.hpp"
#include "one.hpp" //attention about sequence of two headers


There's no issue with building either of these two units - print() only
gets defined once in each. If you link the units together to build an
executable, yes, you are doing something bad, but whether or not the
linker tells you this is a) up to the linker, depending on what options
you give it, and b) off-topic for this group.

May 10 '06 #2
redriver wrote:
Because my english is very poor,so i give my program as follow:
////////////////////////////////////////////////////////////////////////////////////
one.hpp:
#include <iostream>
#ifndef TEST
#define TEST "The first print()"
inline void print()
{
std::cout<<TEST<<std::endl;
}

#else
#undef TEST
#define TEST "the second print()"
inline void print()
{
std::cout<<TEST<<std::endl;
}
#endif /*TEST*/
///////////////////////////////////////////////////////////////////////////////////////////////////
two.hpp:
#define TEST "none"
/////////////////////////////////////////////////////////////////////////////////////////////////
unit1.cpp:
#include "one.hpp"
#include "two.hpp" //attention about sequence of two headers
void doSomething()
{
print();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
unit2.cpp
#include "two.hpp"
#include "one.hpp" //attention about sequence of two headers
int main()
{
print();
}

what we can see there are two "print()"defined,and all in the globe
namespace and
why there is no error about redefine "print()"?


First of all, they are declared 'inline', so the compiler has to make
sure it "merges" all definitions of it into one (if it decides to create
an "out-of-line" definition, that is). The compiler is unable (or maybe
unwilling) to check that your functions have different bodies. It is
a violation of the One Definition Rule, but sometimes violations like
this are subtle and you yourself need to watch for them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 10 '06 #3
Victor Bazarov <v.********@comacast.net> wrote:
First of all, they are declared 'inline', so the compiler has to make
sure it "merges" all definitions of it into one (if it decides to create
an "out-of-line" definition, that is). The compiler is unable (or maybe
unwilling) to check that your functions have different bodies. It is
a violation of the One Definition Rule, but sometimes violations like
this are subtle and you yourself need to watch for them.


I admit I didn't catch the subtleties involved with the use of
"inline". OP's code is still wrong without inline, though, correct?

I suppose the use of "inline" is what led to the linker being quiet;
in my experience, this situation is reported by the linker assuming it
is invoked with reasonable arguments.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 12 '06 #4
Christopher Benson-Manica wrote:
Victor Bazarov <v.********@comacast.net> wrote:
First of all, they are declared 'inline', so the compiler has to make
sure it "merges" all definitions of it into one (if it decides to
create an "out-of-line" definition, that is). The compiler is
unable (or maybe unwilling) to check that your functions have
different bodies. It is
a violation of the One Definition Rule, but sometimes violations like
this are subtle and you yourself need to watch for them.
I admit I didn't catch the subtleties involved with the use of
"inline". OP's code is still wrong without inline, though, correct?


Theoretically. The bodies of the two functions are different. That goes
against the ODR. I don't know of any compiler that would enforce that,
however.
I suppose the use of "inline" is what led to the linker being quiet;
in my experience, this situation is reported by the linker assuming it
is invoked with reasonable arguments.


Then your linker if more advanced than the ones I've seen.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 12 '06 #5

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

Similar topics

2
by: BillD | last post by:
I'm trying to derive a schema from a base schema. I want to redefine a "group" from the base schema in my derived schema in order to add more options to the "choice" aggregate (see schema1.xsd...
1
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm getting a validation error when I try to restrict the content of nested groups with xs:redefine whereas the same restriction on xs:element's...
13
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call...
3
by: junlia | last post by:
We are using ACORD xml schema standard, and we need to add to it, so we choose to redefine ACORD xml schema. One of the problems that I ran into is how to add some values to an emumerated list. ...
25
by: paytam | last post by:
hi all I want to redefine a function getchar() in header stdio.h ,but I don't know what should I do.
2
by: ERingmae | last post by:
Hi, The environment is .NET 2.0, the language is C# and the problem is reading XSD file with xs:redefine section correctly to a XMLDataDocument.DataSet. What I am trying to do: I am trying...
10
by: Amber | last post by:
For example if anywhere defined MyChar as typedef char MyChar; Now I want to redefine MyChar as wchar_t, how can I do this?
16
by: Prayag Narula | last post by:
Hi, I want to redefine fprintf for debugging purposes. That is I want that all the output that is going to the stdout should be logged in a file. I tried something like #define fprintf...
3
by: Ernie.Pasveer | last post by:
Hi All, Is there a way to create a macro called #redefine that will allow redefinition without having to use #ifdef/#undef For example, I'd like to do something like this : #define THING ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.