LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2006, 12:46 AM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Three questions about the head files


1: what's the meaning of explicit? like this:


"explict vector( int theSize =0) : m_CurrentSize( theSize)"

2: Is it correct that include source file at the end of head file? like this:


#ifndef VECTOR_H
#define VECTOR_H

...
..
include "Vector.cpp"
#endif

3:

~vector()
#ifndef WIN32
{
delete[] m_pObjects;}
#else
{
if(m_CurrentSize !=0 )
delete [] m_pObjects;
}
#endif

my question is when and how to set WIN32????

Thanks a lot!
 
Old 06-22-2006, 01:12 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
: what's the meaning of explicit? like this:
"explict vector( int theSize =0) : m_CurrentSize( theSize)"
There are three things going on here:

1. He's declaring parameter "theSize", type int, with default value of "0"

2. He's initializing class member "m_currentSize"
... and, the answer to your main question ...

3. "explict" can be anything. It's probably a "#define" in some header file somewhere.

You should be able to "grep" for it wherever this program's header files live.

Here's one example I found on google (from an STL header file):
Code:
# ifdef __STL_NEED_EXPLICIT
#   define explicit
# endif
In this case, "explicit" is a macro that means <<blank space - nothing at all>>

If somebody had source code like you have above (with "explicit" in the declaration), and "__STL_NEED_EXPLICIT" wasn't defined ... then they'd get a compile error.

Quote:
2: Is it correct that include source file at the end of head file? like this:

#ifndef VECTOR_H
#define VECTOR_H

...
..
include "Vector.cpp"
#endif
Syntactically correct? Yes. Wise? It depends... In this case? It's undoubtedly needed to make sure a client who includes the header (which defines the interface) also has access to the template implementation code (in C++, the compiler needs to see the actual source in order to instantiate the template).

Quote:
3:

~vector()
#ifndef WIN32
{
delete[] m_pObjects;}
#else
{
if(m_CurrentSize !=0 )
delete [] m_pObjects;
}
#endif

my question is when and how to set WIN32????
1. "#define WIN32" in the source will work.

2. "-D" from the command line will also work.

3. "/DWIN32" happens automagically when you use Microsoft's Visual Studio (for example) to build a Windows app.

4. If you're *NOT* compiling for Windows, you probably DON'T want to manually "#define" WIN32. If you *ARE* programming on Windows, it's probably happening for you automatically.

Here's one easy way to check - try compiling this:
Code:
#if defined(WIN32)
  #error I guess WIN32 is defined!
#endif
'Hope that helps .. PSM

Last edited by paulsm4; 06-22-2006 at 01:26 AM.
 
Old 06-22-2006, 03:05 AM   #3
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
Remember that in this case the inclusion of the cpp source file is done because vector is a template, not a standard C++. If write regular C/C++ don't include your source files in you header files!
 
Old 06-22-2006, 06:06 AM   #4
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Just one correction of paulsm4's explanation:
Quote:
Originally Posted by ArthurHuang
1: what's the meaning of explicit? like this:
"explict vector( int theSize =0) : m_CurrentSize( theSize)"
This is (by default) no definition, but a keyword in C++. It was added to the language to bybass a special behavior of some constructors: A constructor that takes only one argument (maybe followed by others with default-values) also acts as an implicit conversion operator. Example:
Code:
class Foo{
public:
  Foo (int i) : m_i(i) {}
private:
  int m_i;
};

int main(){
  Foo f = 1; //implicit converts '1' to a Foo-object and assignes it to f
}
Such a behavior is (most) often not wanted, that's the gig of explicit. If you modify such constructors with explicit the compiler won't do implicit converstions any more and will throw errors instead:
Code:
class Foo{
public:
  explicit Foo (int i) : m_i(i) {}
private:
  int m_i;
};

int main(){
  Foo f = 1; //error: conversion from `int' to non-scalar type `Foo' requested
}
The correct code would then be:
Code:
//...
int main(){
  Foo f = (Foo)1; //explicit conversion, that's OK!
}
So every such constructor that should not act like an implicit conversion operator, should always be modified with explicit.
 
Old 06-22-2006, 12:38 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Thanx, Flesym - you're absolutely correct.

Here's another explanation, from MSVC++:
Quote:
explicit
C++ Specific

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. Constructors declared explicit will not be considered for implicit conversions. For example:

Code:
class X {
public:
   explicit X(int);      //legal
   explicit X(double) {   //legal
      // ...
   }
};

explicit X::X(int) {}      //illegal
An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above:

Code:
void f(X) {}
void g(int I) {
   f(i);      // will cause error
}
void h() {
   X x1(1);      // legal
}
The function call f(i) fails because there is no available implicit conversion from int to X.

Note
It is meaningless to apply explicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions.
Not all C++ compilers necessarily support the "explicit" keyword (it was added in 1998, I believe), hence the need for the "#define explicit" macro in the STL.

Last edited by paulsm4; 06-22-2006 at 12:40 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dual head n00b questions PsychosisNode Linux - Hardware 2 05-30-2005 10:51 AM
Multi-monitor Issues on RH9, Geforce 4 Ti Dual Head + TNT2 Single Head the letter b Linux - Newbie 3 12-04-2004 11:23 PM
a few questions about files Greg_courageous Linux - Newbie 5 04-20-2004 08:57 AM
Questions about device files jironimo Linux - Newbie 1 03-24-2004 04:59 PM
x86 Solaris 9 XSun and Matrox G550 dual-head... one head down, one to go. finegan Solaris / OpenSolaris 4 03-11-2003 12:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration