LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-07-2011, 11:58 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
extern <class>?


Hi, normally I write my code with functions of a similar nature in one .c file, and then compile it into .o file so I can link it with the main .o file if required. Doing so I'd declare the functions I need to be linked ito the main file by using the phrase "extern <function prototype>".

However, if these are not functions but are classes that I need to instantiate from the main file, do I sill use something like "extern <class name>"? I think I read it somewhere saying it doesn't work like this?
 
Old 11-08-2011, 05:23 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Look into prototyping (declaring) your function(s) and/or class(es) in one or more header files. You should have been doing that when you programmed in C, and this practice should continue when developing in C++.

For example...

Foo.h:
Code:
#ifndef FOO_H
#define FOO_H

class Foo
{
public:
   Foo(int val);

   int getValue() const;

private:
   int value;
};

#endif
Foo.cpp:
Code:
#include "Foo.h"

Foo::Foo(int val)
   : value(val)
{
}

int Foo::getValue() const
{
   return value;
}
Main.cpp:
Code:
#include "Foo.h"
#include <iostream>

int main()
{
   Foo foo(5);

   std::cout << "foo's value is: " << foo.getValue() << std::endl;
}
To build these:
Code:
g++ Foo.cpp Main.cpp -o foo

# OR

g++ -c Foo.cpp
g++ -c Main.cpp
g++ Foo.o Main.o -o foo

Last edited by dwhitney67; 11-08-2011 at 05:25 AM.
 
Old 11-08-2011, 07:46 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> However, if these are not functions but are classes that I need to instantiate from the main file, do I sill use something like "extern <class name>"?

No, simply do one of these:

1. #include "classname.h"
2. class classname;

If you choose the latter, you will be able to create pointers to class classname, eg:

classname *myclassnameptr= NULL;
 
Old 11-08-2011, 06:59 PM   #4
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Hi, thank you very much to the both of you! 3 Questions:
1) Regarding the following block of code in Foo.cpp.
Quote:
Foo::Foo(int val)
: value(val)
{
}
Can I do the following? I have not come across the above way before.
Quote:
Foo::Foo(int val)
value = val
{
}
2) If the class Foo needs to inherent another class (say Mike), do I need to specify
Quote:
#include "Mike.h"
Inside each of the following:
Foo.h, Foo.cpp, Main.cpp?

3) If we use
Quote:
class <classname>
Rather than
Quote:
#include "classname.h"
What benefits does it bring with the ability to use
Quote:
classname *myclassnameptr= NULL;

Last edited by grob115; 11-08-2011 at 07:02 PM.
 
Old 11-08-2011, 11:02 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> Foo::Foo(int val) value = val {}

I guess it is syntax error, but this is okay:
Foo::Foo(int val) { value = val; }

> Inside each of the following: Foo.h, Foo.cpp, Main.cpp?

If Foo.h includes Mike.h, then files including Foo.h don't have to include Mike.h themself (but they may -- multiple inclusion is allowed).

> class <classname>

Syntax error, use:
class Mike;

It means: there is somewhere a class called Mike, but right now we don't have to know anything about its details, because we only want to create a pointer to it.
 
Old 11-09-2011, 05:58 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
@ grob115 (and to some extent, NevemTeve)...

The syntax that I posted earlier is correct to initialise data member of a class; to clarify:
Code:
<classname>:<classname>(<args>)
  : <member data initialization area>
{
   <body of constructor>
}
If you have not come across this syntax in your studies, then I would suggest that you procure another study guide/book. It is a widely used concept in C++.

An example that you can practice on is not only the one I provided earlier, but also the following:
Code:
class Foo
{
public:
   Foo(int& val);

   int getValue() const;

private:
   int& value;   // here, we are defining a reference to an int
};

Foo:Foo(int& val)
   : value(val)     // this MUST be initialised here
{
   // the compiler will NOT allow value to be initialised here
}

int Foo::getValue() const
{
   return value;
}

#include <iostream>
int main()
{
   int val = 5;
   Foo foo(val);

   // will see value of 5 after output is complete
   std::cout << "foo's value is: " << foo.getValue() << std::endl;

   val = 10;

   // will see value of 10 after output is complete
   std::cout << "foo's value is: " << foo.getValue() << std::endl;
}
As for your other question concerning forward declarations, typically these are done within header files. They can be used when a pointer or a reference to a class is required. One of the benefits is to speed up the compilation of modules, the other is to resolve "chicken-egg" problems where one class may depend upon another that is yet to be defined.
 
Old 11-13-2011, 04:35 AM   #7
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Okay thanks. IBM's reference has a good description.
Quote:
Constructors can initialize their members in two different ways. A constructor can use the arguments passed to it to initialize member variables in the constructor definition:

complx(double r, double i = 0.0) { re = r; im = i; }

Or a constructor can have an initializer list within the definition but prior to the constructor body:

complx(double r, double i = 0) : re(r), im(i) { /* ... */ }

Both methods assign the argument values to the appropriate data members of the class.
However, I can't see the advantage of one over the other. If there is one, please comment. Thanks.
 
Old 11-13-2011, 04:49 AM   #8
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Okay guess using a member initilization list is better as per this.

Last edited by grob115; 11-13-2011 at 06:13 AM.
 
  


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
[SOLVED] C++ Initialize Class Member Variables of Another Class Type mirlin510 Programming 9 04-13-2011 11:46 AM
[SOLVED] Python n00b: promoting a derived class to base class? CoderMan Programming 2 03-11-2010 01:46 PM
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM

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

All times are GMT -5. The time now is 04:54 AM.

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