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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:32:35+00:00 2026-05-26T20:32:35+00:00

in C# I created a static class which had a number of mathematical helper

  • 0

in C# I created a static class which had a number of mathematical helper functions I could call directly without creating an instance of the class. I cannot seem to get this to work in C++.

For example, if the class is called MathsClass and has a function called MultiplyByThree then I would use it like this:

float Variable1 = MathsClass.MultiplyByThree(Variable1);

In the C++ version of my code I am getting the errors:

 'MathsClass' : illegal use of this type as an expression

and

 error C2228: left of '.MultiplyByThree' must have class/struct/union

How would I write the C++ equivalent of the C# static class to give this kind of functionality?

  • 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-26T20:32:35+00:00Added an answer on May 26, 2026 at 8:32 pm

    The easy answer is to use the :: operator instead of the . operator:

    float Variable1 = MathsClass::MultiplyByThree(Variable1); 
    

    But in C++, free functions are generally preferred over static class functions, unless you have a specific reason to put them in a class. For keeping them together, and not polluting the global namespace, you can put them in their own namespace:

    In Math.h

    namespace Math
    {
        float MultiplyByThree(float x);
    }
    

    In Math.cpp:

    #include "math.h"
    
    namespace Math
    {
        float MultiplyByThree(float x)
        {
            return x * 3;
        }
    }
    

    And to use it:

    #include "math.h"
    
    float Variable1 = Math::MultiplyByThree(Variable1); 
    

    Even better, make it a template and the same code will work for floats, doubles, ints, complex, or any type that has operator* defined:

    In Math.h

    namespace Math
    {
        template <typename T>
        T MultiplyByThree(T x)
        {
            return x * 3;
        }
    }
    

    The only issue being that you can’t separate the definition into math.cpp, it has to be in the header.

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

Sidebar

Related Questions

I intended to create a class which only have static members and static functions.
I'm trying to create a generic class which will have some static functions based
I have created a public static class utils.cs I want to use it in
For example I now created a this tiny class: public static class FileSystemInfoComparers<T> where
I have created my own Attached Property like this: public static class LabelExtension {
I am trying to find why the class cant be created as a static?
I'm trying to access a static method in a Utils class I created: public
I had created a Proxy Server (Fwd and Reverse) using Java sockets. which would
I have created a C++-wrapper (a class) for a larger C codebase which was
I created a class which is used as a cache provider. It uses a

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.