ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
> 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:
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.
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.