1

Can someone tell me what is the correct way to create a .hpp and .cpp that includes classes, subclasses and methods? Do I have to use export "C" firstClass* create_object { return new firstClass; }? (I am working in C++.) Should I have file.hpp or file.h?

#include <string>

//public ?? how can i have this?
class firstClass
{
public:
    firstClass();

    class secondClass
    {
    public:
        secondClass();
        std::string name;
        virtual std::string method1();
    } *sec;

    virtual void DoSomething();
} *first;

// And for a private class?

class private *priv;

in file.cpp

#include file.hpp
firstClass::firstClass()
{
    sec = new firstClass::secondClass();
}

std::string firstClass::secondClass::method1()
{
    //code
}

And now if I have to extern an object for each class/subclass? It is necessary if I want to create an .so file and use dlsym and dlopen to access classes, subclasses and methods, modify values, send a reference to a specific method? Thanks!

extern "C" firstClass* create_object()
{return new firstClass}
extern "C" secondClass* create_object()
{return new secondClass}
quamrana
  • 37,849
  • 12
  • 53
  • 71
linuxx
  • 1,487
  • 4
  • 16
  • 17
  • 1
    Is C++ your first programming language... It seems you are confusing the syntax with some other language. Take a look at: http://www.cplusplus.com/doc/tutorial/classes/ – RedX May 05 '11 at 09:26
  • 2
    Please, read [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), you're missing the full basics – rubenvb May 05 '11 at 09:36
  • I am not a beginner and i do need some help:). I am new to stuff like .h files and how to export classes. – linuxx May 05 '11 at 09:42
  • header files are beginner stuff. –  May 05 '11 at 09:55

1 Answers1

7

Whether you have file.hpp or file.h is just a matter of preference. Personally, I use .hpp to indicate C++ rather than C.

In C++, unlike for example Java, classes in the global namespace are not public or private. You can, however, hide or limit access to classes in various ways. For example:

  • Having them private in another class.
  • Using namespaces.

A few other points:

  • The syntax for including file.hpp is #include "file.hpp"
  • Your firstClass and secondClass classes have virtual methods and a default destructor. You should make your destructor public and virtual, or protected and non-virtual
  • Classes are usually defined like this:

Class definition:

class SomeClass {
    // code
};

and when you want a pointer to it, you write SomeClass* myPointer;, rather than the way you declare first and sec.

You usually want to put class declarations and/or definitions in header files, along with function declarations. Implementations goes in the source files. Example:

// Header file:
class FirstClass {
public:
    FirstClass();
    void DoSomething();
};

// Source file:
FirstClass::FirstClass()
{
    // code
}

void FirstClass::DoSomething()
{
    // code
}

I'd recommend you to pick up a C++ textbook, where such basics are plentifully covered. My personal recommendation is Accelerated C++.

Lstor
  • 2,265
  • 17
  • 25