Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:38:14+00:00 2026-05-28T06:38:14+00:00

I have a class in the file AType.h and it is implemented in AType.cpp.

  • 0

I have a class in the file AType.h and it is implemented in AType.cpp.

# include "PrivateType.h"

class AType{
    private:
    int a, b, c;
    PrivateType varX;

    public:
    ...
};

I want to use the class AType in the file main.cpp and I will need to include AType.h, but I want to avoid the inclusion of PrivateType.h in main.cpp.
I cannot create varX with malloc/new.
main.cpp have to know the size of AType at compile-time.

Current solution: (It is bad)

1 – Create a program to print sizeof(AType).
2 – Change the header:

# ifdef ATYPE_CPP
    # include "PrivateType.h"
#endif

class AType{
    private:
    # ifdef ATYPE_CPP
        int a, b, c;
        PrivateType varX;
    # else
        char data[ the size that was printed ];
    # endif

    public:
    ...
};

3 – And AType.cpp will start with:

# define ATYPE_CPP
# include "AType.h"

Edit 1

Is there a way or tool to automatically change a complex struct into C primitive types?
I do not want to have to open header files and find the structs.

If PrivateType is:

struct DataType {
    float a, b;
};
class PrivateType {
    void* a;
    int b;
    short c;
    DataType x;

    ... functions
};

AType would be changed to:

class AType {
    int a, b, c;
    struct x {
        void* a;
        int b;
        short c;
        struct x2{
            float a, b;
        };
    };
};

And I would handle copy/equality methods separately.
I use GCC or Clang.

Edit 2
A new solution?

It is for GCC.

1 – Get sizeof(AType) and __alignof__(AType).
2 – Change the header:

# ifdef ATYPE_CPP
    # include "PrivateType.h"
#endif

class AType{
    private:
    # ifdef ATYPE_CPP
        int a, b, c;
        PrivateType varX;
    # else
        char data[ 'the sizeof(AType)' ];
    # endif

    public:
    ...
}
# ifdef ATYPE_CPP
    ;
# else
    __attribute__ (( aligned( 'The __alignof__(AType)' ) ));
# endif

3 – Write all copy/equality methods in AType.cpp.

Will it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:38:15+00:00Added an answer on May 28, 2026 at 6:38 am

    You cannot do what you want (because you ruled out dynamic allocation), and your “solution” does not work in general, even if you avoid the problems with compiler-generated special member functions which others mentioned. One problem is that types not only have a size, but also an alignment. For example, your real class contains an int, however your replacement class contains only a char array. Now on most platforms, int has alignment 4 (i.e. an int must be located at a 4 byte boundary), while char has aligment 1 (it cannot have any other alignment without violating the standard). That is, as soon as you try to create an object with your replacement definition, you risk getting it misaligned, which in the best case will cause a massive slowdown, in the worst case a crash of your program (and in the absolutely worst case, it will work in your tests, but fail when actually used).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure which is as given below: class File { public
I have the following windows service file: Imports System.ServiceProcess Imports System.IO Public Class fswService
I have a class file to handle the GUI which is SampleGUI.class. I want
I have included #include </usr/include/c++/4.4.3/tr1/shared_ptr.h> in my class file, When I attempt to compile
I have a class file that contains all the classes that are needed for
I currently have a class file with the following enumeration: using System; namespace Helper
We have a class Car defined like this in a car.rb file class Car
I have created a C# class file by using a XSD-file as an input.
I used to have one class for one file. For example car.cs has the
I have a function in a class file called auth_user and its in App_code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.