Avoiding Dependencies #2

Avoiding Dependencies #2

Many C++ programmers will be familiar with the tidal wave of recompilations that occur when a seemingly unrelated header file is changed. C++ file dependencies need to be managed otherwise compilation times will grow unchecked. Large build times can cripple the process of creating software and eventually kill a project.

Suppose the class fubar uses the class snafu. Which uses require a #include in fubar.hpp and which ones don’t? Here’s a stylized code fragment that exhibits all possible uses (except inheritance which does require the #include).

 // fubar.hpp#include class fubar{public: // parameter type    void method1(snafu   value_parameter);     // 1     void method2(snafu * pointer_parameter);   // 2    void method3(snafu & reference_parameter); // 3public: // return type    snafu   value_return();     // 4    snafu * pointer_return();   // 5    snafu & reference_return(); // 6private: // object state      snafu   value;     // 7    snafu * pointer;   // 8    snafu & reference; // 9    private: // class state    static snafu   shared_value;     // 10    static snafu * shared_pointer;   // 11    static snafu & shared_reference; // 12};


The only one that requires a #include is number 7. A forward declaration will do for all the rest.

  // fubar.hppclass snafu;class fubar{public: // parameter type    void method1(snafu   value_parameter);     // 1     void method2(snafu * pointer_parameter);   // 2    void method3(snafu & reference_parameter); // 3public: // return type    snafu   value_return();     // 4    snafu * pointer_return();   // 5    snafu & reference_return(); // 6private: // object state      //snafu   value;   // 7    snafu * pointer;   // 8    snafu & reference; // 9    private: // class state    static snafu   shared_value;     // 10    static snafu * shared_pointer;   // 11    static snafu & shared_reference; // 12};


However, be aware that you can’t forward declare a class when:

  • The class is a nested class (there is no legal syntax to do it!)
  • The class lives in the standard library (prohibited by ISO C++)
  • The class is not a class! (e.g. a typedef’d template)
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as