0

I am working on a codebase that is not my own, that has the following layout:

object.h:

// Objects are defined
// #include "tickets.h" (functions to access the objects)
// An access-handler object is defined

I want to introduce a class that knows about the objects, can be accessed from functions in tickets.h, but can also use the access-handler object. The functions are separate, i.e. class functions that are called in tickets.h do not use the access-handler (I wouldn't know where to start if that weren't the case).

Therefore my class needs to be defined before tickets.h, but some of its functions need to be defined after the access-handler. Is there a way to do this without splitting it up into two header files something like the following:

// Objects are defined
//  -- include declaration of class, and definition of functions that tickets.h needs
// #include "tickets.h"
// An access-handler object is defined
//  -- include functions of class that need the access-handler

This seems very messy splitting things up like this into two separate files, I was hoping to keep everything contained.

Thanks for any help, I clearly only have a very rudimentary understanding of declarations/definitions in c++.

EDIT: If I use forward declaration and include it before tickets.h (with the class declared in mynewclass.h and functions defined in mynewclass.cc) will mynewclass.cc be able to use objects declared after the inclusion of mynewclass.h? Namely the access-handler object.

EDIT2: Something like this:

object.h:

class obj { // definition }

#include "tickets.h"

class obj_handler {
    public void handle { // code }
}

tickets.h:

void do_something(obj o){
    communicator.foo();
}

My object (communicator):

class communicator {
    public:
        void foo() { // code } 
        void bar() { // use handle() from obj_handler }
}

As you can see, my communicator needs to be used in tickets.h, but can't be defined until after obj_handler. So where should I include it?

ricky116
  • 744
  • 8
  • 21
  • Can you give a minimal example in code? Because I don't understand the question at all. For example, why would you define objects in a header file? You'll end up with multiple copies of those objects in each translation unit. Unless you mean something different, in which case only example code can help us understand what you mean. – Nikos C. Jul 27 '13 at 16:16
  • I am introducing new functionality into a system that isn't my own, where most of the functions are actually implemented in the header files. I guess I can attempt a small section of code that explains what I mean. – ricky116 Jul 27 '13 at 16:29

2 Answers2

2

If I correctly understand your question - you can use forward declaration to solve this problem. This will allow you to declare some class before defining it's methods. For example:

// this is forward declaration of class A, no definition provided
class A;

class B
{
// uses A
A * a_;
};

// class A definition
class A
{
// may use class B now
B * b_;
};
1

I'm not quite sure whether I understand this right and don't have enough reputation here yet to make this a comment, so let me try to answer your question this way, please feel free to follow up if I'm guessing wrong:

I believe what you are referring to is an entire class definition, i.e., one including all function definitions within the class declaration. Other than that, it is not very common to see object definitions followed by preprocessor directives. What is typical though is a forward declaration of functions and a class prototype.

So, for example, you could declare in some header.h:

class C 
{
public:
    void method1(void);
    int method2(void);
};

And in some implementation.cpp the definition of the functions like:

void C::method1(void) { /*...*/ }

In the other file preceded in the inclusion chain by your access-handler you then define the other function:

int C::method2(void) { /*...*/ }

What do you mean by access-handler, by the way?

Oh, and your linker likely will yell somewhat at you if you do function definition in a header file.


With regard to your addenda: everywhere you put a forward declaration, loosely speaking, the compiler will insert a copy of the declaration in question, consider it a soft link in the context of file systems. There are negative implications associated with it, like increased duration and the memory load of compilation if you have many forward declarations of the function signature or class. It's impossible to tell whether this will word in your particular situation since only you know the actual code in question. But most likely it would work.

Take a look at these pages:

  1. http://en.wikipedia.org/wiki/Forward_declaration
  2. When can I use a forward declaration?
Community
  • 1
  • 1
Nicholas
  • 133
  • 1
  • 1
  • 7
  • or also what whale-calf writes – Nicholas Jul 27 '13 at 16:23
  • I'm limited to positing only 2 links per answer, here's the rest of the list: 3. http://www.chromium.org/developers/coding-style/cpp-dos-and-donts 4. http://www.learncpp.com/cpp-tutorial/19-header-files/ – Nicholas Jul 27 '13 at 17:25