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

static function definition

Hi all,
I was thinking that all static members(both data members and functions)
should be defined.
I try to use a static function without defining it
1. outside the class - it gives an error and thats is what i expected.
2. inside the class, ie inside another member function, but it compiles.Why
is that??

Does not the compiler need the definition of Init to generate the code for
Foo ???
I looked into the TCPPPL but didn't find an answer...Can somebody kindly
explain where my understanding
goes wrong ???

#include <iostream>

using namespace std;

class A
{
public:
static void Init();
void Foo() { Init(); }
};

int main()
{
A::Init(); --->Gives an error that Init is an unresolved external symbol.
A a;
a.Foo(); ---> Fine,Also gives an error.
return 0;
}
Here is the actual code that started my confusion..
template<class B,class D> class IsDerivedFrom1
{
class No{};

class Yes{ No no[2]; };
public:
static Yes Test(B* pb);
static No Test(...);

public:
enum { Is = sizeof(Test(static_cast<D*>(0))) > sizeof(No) };

};

Is the declaration of Test is enough to resolve the overload in the
initialisation of the
enum value Is ???

Thanks and Best Regards,
Senthilvel.
Jul 22 '05 #1
1 2355

"Senthilvel" <Se*****@nospam.com> wrote in message
news:cb**********@news.mch.sbs.de...
Hi all,
I was thinking that all static members(both data members and functions)
should be defined.
I try to use a static function without defining it
1. outside the class - it gives an error and thats is what i expected.
2. inside the class, ie inside another member function, but it compiles.Why is that??

Does not the compiler need the definition of Init to generate the code for
Foo ???
I looked into the TCPPPL but didn't find an answer...Can somebody kindly
explain where my understanding
goes wrong ???

#include <iostream>

using namespace std;

class A
{
public:
static void Init();
void Foo() { Init(); }
};

int main()
{
A::Init(); --->Gives an error that Init is an unresolved external symbol.
A a;
a.Foo(); ---> Fine,Also gives an error.
return 0;
}
Here is the actual code that started my confusion..
template<class B,class D> class IsDerivedFrom1
{
class No{};

class Yes{ No no[2]; };
public:
static Yes Test(B* pb);
static No Test(...);

public:
enum { Is = sizeof(Test(static_cast<D*>(0))) > sizeof(No) };

};

Is the declaration of Test is enough to resolve the overload in the
initialisation of the
enum value Is ???


Yes it is. If you don't actually call Test, you don't need to define it.

john
Jul 22 '05 #2

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

Similar topics

24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
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 declared static instead classes 4. objects declared...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.