How to include class definition in 2 header files?

I need to split one class (.h file)

#ifndef _L_H
#define _L_H
template<class L> class Myclass{
public:
  L();
  firstoperator(..);
  secondoperator(..);
private:
 ...
}
template <class L> Myclass<L>::L() ...
template <class L> Myclass<L>::firstoperator(..) ...
template <class L> Myclass<L>::secondoperator(..) ...

      

in two different .h files in the following form:

#ifndef _L_H
#define _L_H
template<class L> class Myclass{
public:
  L();
  firstoperator(..);
private:
 ...
}
template <class L> Myclass<L>::L() ...
template <class L> Myclass<L>::firstoperator(..) ...

      


#ifndef _L_H
#define _L_H
template<class L> class Myclass{
public:
  secondoperator(..);
}

template <class L> Myclass<L>::secondoperator(..) ...

      

how can i do this correctly without conflicts?

Thanks in advance.

+3


source to share


3 answers


You can use legacy to split a class into two headers.

You can declare half of the class on the base and the other half on the base.

like this:

class C12{
public:
  void f1();
  void f2();
};

      



can be divided into C1 and C12

class C1{
public:
  void f1();
};

class C12: public C1{
public:
  void f2();
};

      

now C12 is the same as before but split into 2 files.

+1


source


You can technically do this, but it's ugly. Don't do this .

Class1.hpp:

class MyClass
{
    int something;
    int somethingElse;

      



Class2.hpp:

    int somethingBig;
    int somethingSmall;
};

      

Hope it's clear how disgusting it is :)

+8


source


"how can I do it right without conflicts?"

You can not. It is not possible in C ++ to propagate a class declaration across multiple header files (unlike in C #). All class member declarations must appear within the body of the class declaration, at one point visible in every translation unit that uses the class.

You can split specializations or template implementations to split the header.

+3


source







All Articles