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 8175359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:45:06+00:00 2026-06-06T22:45:06+00:00

I use a global variable (an object of a class) and define it as

  • 0

I use a global variable (an object of a class) and define it as follows:

//foo.h
extern class_name obj_name;
//foo.cpp
class_name obj_name("directory of a .ttf file");

I included foo.h in my main.cpp where my main() function resides.

The problem is that I am not very comfortable with initializing it there , since it is not inside a function. It’s out in the open. I would have preferred calling an “init()” function that initializes the object like:

object_name = class_name(parameters);

from the main() function.

But the class is a 3rd party one and it doesn’t accept objects being created without us providing parameters for the constructor. Maybe it doesn’t have a default no-parameter constructor.

Is it good coding style to do what I did above?

  • 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-06-06T22:45:07+00:00Added an answer on June 6, 2026 at 10:45 pm

    No.

    Don’t use globals in C++ without very good reason.

    • You’ll run into the “static initialisation order fiasco”.
    • You’ll get ugly spaghetti code.
    • Your code will be difficult to test.

    This is especially true if your global is of a 3rd party type.

    Consider using something nicer like a factory method allowing lazy initialization.
    Or just pass the object around to functions that need it.

    Also consider wrapping that 3rd party class with your own – only providing the minimal interfaces needed to test it, and use a pointer to that abstract base class. (Or if the 3rd party library supports it – use the most minimal interface they provide that you can get away with using)

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

Sidebar

Related Questions

Example : Say I have a logging class, and I use a global variable
I use global variable $table_prefix to differ whether I work on Word Press or
As I know, I can use global variable transfer the value among the multi
I want to use a global variable in inline assembly. asm( LDR R0,g_TsInitStackPointerAddress); Here
I'm trying to use a global variable. I've declared it as global to begin
I'm writing code in Visual C++ and I need to use a global variable.
i declare a global variable and use and modify its value in the function.
I'm trying to use usort() and leverage a global variable inside its function scope,
Suppose I have a program that initializes a global variable for use by threads,
In Delphi 2007 and later, the global variable UseLatestCommonDialogs causes TOpenDialog to use the

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.