473,396 Members | 1,900 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.

Is overriding a function of a library in accordance with C++ standard?

Is overriding a function of a library in accordance with C++ standard?

The following code are passed by the VS 2005 and Dev C++.

#include <cstdlib>
#include <iostream>

using namespace std;

size_t strlen(const char* p)
{
return 0;
} // !!! Note this !!! The standard library function strlen is
deliberately overriden.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?

Any help will be appreciated. Many thanks in advance.

Aug 25 '06 #1
7 6586
Lighter wrote:
Is overriding a function of a library in accordance with C++ standard?
You can't override an existing function because that is violation to the
One Definition Rule (ODR.)

More explanation below.
>
The following code are passed by the VS 2005 and Dev C++.

#include <cstdlib>
#include <iostream>

using namespace std;

size_t strlen(const char* p)
{
return 0;
} // !!! Note this !!! The standard library function strlen is
deliberately overriden.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?
Nope. The standard library strlen resides in the std namespace. The
using directive merely makes such a name available in the global scope.
Your own version of strlen is declared in the global scope and so hides
the std::strlen.

To truly override strlen and get a compiler error you can do the following:

#include <cstring>

namespace std{
size_t strlen(const char*){return 0;}
}

int main()
{
strlen("oops!");
}
>
Any help will be appreciated. Many thanks in advance.
Regards,
Ben
Aug 25 '06 #2
benben wrote:
Nope. The standard library strlen resides in the std namespace. The
using directive merely makes such a name available in the global scope.
Your own version of strlen is declared in the global scope and so hides
the std::strlen.

To truly override strlen and get a compiler error you can do the following:

#include <cstring>

namespace std{
size_t strlen(const char*){return 0;}
}

int main()
{
strlen("oops!");
}
Dev C++ can still normally compile the code, but VS 2005 report an
error.

Aug 25 '06 #3
"Lighter" <cq****@gmail.comwrote in message news:11**********************@p79g2000cwp.googlegr oups.com...
Is overriding a function of a library in accordance with C++ standard?

The following code are passed by the VS 2005 and Dev C++.

#include <cstdlib>
#include <iostream>

using namespace std;

size_t strlen(const char* p)
{
return 0;
} // !!! Note this !!! The standard library function strlen is
deliberately overriden.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?
I am not sure what the standard says about it. In practice this works except
if other functions in the run-time library are used in the program.
On many platforms, the run-time functions are pre-linked.
(Linux uses shared object files (.so) .
Windows uses dynamic linked libraries (.dll)
VMS uses shareable images (.exe))
In such cases the standard version of the function is present in the pre-linked
file and will be used internally.
In order to use your own version of the function, special link options must
be use to prevent the use of the pre-linked run-time library. This will make
the resulting executable file much larger, as it now contains all run-time
library functions used by the program. In addition these functions are no
longer shared with other programs, so that the program may use more memory.
Aug 25 '06 #4
neo
I am using vs2k5. Your code is working perfectly; in your code I
include iostream.h in your code, its give error as following:

e:\vc\bug\bug\bug.cpp(12) : error C2883: 'std::strlen' : function
declaration conflicts with 'strlen' introduced by using-declaration
c:\program files\microsoft visual studio
8\vc\include\string.h(80) : see declaration of 'strlen'

If I want to use iostream.h, how we would be override strlen function?

In my understanding strlen function will not be override because it
will be conflict between two functions in std namespace, if it is
possible to override std function, I am interested to learn it.

Regards,
-aims
benben wrote:
Lighter wrote:
Is overriding a function of a library in accordance with C++ standard?

You can't override an existing function because that is violation to the
One Definition Rule (ODR.)

More explanation below.

The following code are passed by the VS 2005 and Dev C++.

#include <cstdlib>
#include <iostream>

using namespace std;

size_t strlen(const char* p)
{
return 0;
} // !!! Note this !!! The standard library function strlen is
deliberately overriden.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?

Nope. The standard library strlen resides in the std namespace. The
using directive merely makes such a name available in the global scope.
Your own version of strlen is declared in the global scope and so hides
the std::strlen.

To truly override strlen and get a compiler error you can do the following:

#include <cstring>

namespace std{
size_t strlen(const char*){return 0;}
}

int main()
{
strlen("oops!");
}

Any help will be appreciated. Many thanks in advance.

Regards,
Ben
Aug 25 '06 #5
neo wrote:
I am using vs2k5. Your code is working perfectly; in your code I
include iostream.h in your code, its give error
That's to be expected; its <iostream.his not a standard C++ header
but a microsoft-specific one to help their customers deal with
pre-standard
C++ code. Any error you get with it has nothing to do with standard
C++, the topic here. Try the microsoft.* newsgroups.

HTH,
Michiel Salters

Aug 25 '06 #6
In article <11**********************@p79g2000cwp.googlegroups .com>,
cq****@gmail.com says...
Is overriding a function of a library in accordance with C++ standard?
Sometimes. Just for example, you are specifically allowed to supply your
own memory management functions. There are rather tight restrictions on
what you can add to namespace std though.
#include <cstdlib>
#include <iostream>

using namespace std;

size_t strlen(const char* p)
{
return 0;
} // !!! Note this !!! The standard library function strlen is
deliberately overriden.
Only sort of. The standard library has a function named "std::strlen".
If you include <cstringthat's how it's visible. If you include
<string.h>, it's been made visible in the global namespace (as if by
"using std::strlen"), but its name is still "std::strlen", not
"::strlen".

As such, what we have are two different functions with two different
names, but a mechanism (or two, technically -- the using declaration and
the using directive) that allows us to ignore that particular difference
in the name.

That means your name doesn't directly collide with the one in the
standard library. You're not allowed to do this though:

namespace std {
size_t strlen(const char *p) {
return 0;
}
};

Now, one other minor detail: in C++, any standard header is allowed to
include any other standard header. That means that even though you
haven't included either <cstringor <string.h>, it's possible that
having included <iostreamand <cstdlibcould include <cstringor
<string.h>. In that case, the name in your code would collide with the
name from the standard library, and the compiler would reject your code.

Most of the rest of your summary about how compilation works is
generally correct in general, but not required -- and while most
compilers work about as you described, there are a few that work
substantially differently (e.g. the last time I looked at it, IBM VA C++
took a somewhat more global approach to things. I never tested it with
code like yours but I wouldn't be surprised if it reacted somewhat
differently to your code).

To summarize: the compiler is allowed but not required to reject your
code. As an aside, I'd also add that C has substantially stricter rules
in this respect than C++. In C all names starting with 'str' are
reserved, so code like your's would have undefined behavior.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 25 '06 #7
neo wrote:
I am using vs2k5.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian (free pizza mellows the engineer)
Aug 25 '06 #8

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

Similar topics

9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
0
by: Ryan Mack | last post by:
I'm doing development on an embedded system using a GCC 2.96 MIPS cross compiler and a minimal C standard library replacement. The system loads at startup a base executable. The base executable...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
3
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static...
6
by: Eric | last post by:
I have a program that uses a custom function that, as it happens, has the same name as a function in the standard library. By custom, I mean it was something that was written in-house because at...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
12
by: pantagruel | last post by:
Hi, I'm thinking of making a WScript based JavaScript library, I can think of some specific non-browser specific scripting examples that should probably make it in, like Crockford's little...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
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.