-1

EDIT: No... I dont wanna use :: I want to be able to use . when using static method EDIT 2: Not a namespace. A class/struct is what I'm trying to use!

I bet this has been asked and answered before but I cant find an answer

Suppose I have a class called "A"

struct A {
    void log(const char* x) {
         std::cout << x << std::endl;
    }
}

If i want to use this class i need to do something like A B; Then i can do something like B.log("Hello World!");

But is there any possible way I dont have to make an object to use a class? I just want to be able to do A.log("Hello World!");

Is that possible?

Duffka
  • 9
  • 2
  • 1
    You are confusing class with struct. Yes, off-course you can call methods directly. Just use the magic word `static` in function definition – Abhishek Nov 10 '20 at 23:41
  • 1
    ... and then it would be `A::log("Hello World!");` – Paul Sanders Nov 10 '20 at 23:42
  • @PaulSanders So using static makes me have to use :: instead of . ? – Duffka Nov 10 '20 at 23:43
  • 2
    @Abhishek I thought the only difference was struct was public by default – Duffka Nov 10 '20 at 23:44
  • 1
    @Abhishek OP is correct in their usage. Aside from the default accessibility, `class` and `struct` are the same. – Ken Wayne VanderLinde Nov 10 '20 at 23:44
  • 1
    Agree, but in the question OP was using struct and calling it as a class :-) I was just pointing it out. – Abhishek Nov 10 '20 at 23:46
  • @Duffka _So using static makes me have to use :: instead of ._ If you have an instance of the object, you can use `instance.log()`. If not, you can use the class name as I showed above. – Paul Sanders Nov 10 '20 at 23:47
  • @Abhishek Well, structs are classes, so there appears to be no confusion on OP's part regarding that. P.S. unions are classes too. – eerorika Nov 10 '20 at 23:54
  • Referencing another question, as it looks relevant for the use case of (dot) https://stackoverflow.com/questions/4731406/dot-access-to-static-method – Abhishek Nov 11 '20 at 00:00
  • @eerorika strictly speaking it isn't. I mean private and public access identifier makes a difference :-) – Abhishek Nov 11 '20 at 00:01
  • @Abhishek Strictly speaking it is. Struct is a class that is defined with the class-key `struct`. The default access specifier of classes defined with class-key `class` is different from the default access specifier of classes defined with the class-key `struct` or `union`, but that doesn't make the latter non-classes. – eerorika Nov 11 '20 at 00:04
  • What is a class-key? – Abhishek Nov 11 '20 at 00:05
  • 2
    @Abhishek class-key is part of the grammar of the class definition. The simplified grammar is `class-key attribute-specifier-seq class-head-name class-virt-specifier base-clause { member-specification }`. The grammar of class-key is `class | struct | union`. All language rules that apply to classes also apply to structs and unions, because they are also classes - unless those rules explicitly exclude structs or unions. It is always correct to say that a struct is a class. – eerorika Nov 11 '20 at 00:11
  • Unbelievable!! Thanks for sharing this with me. – Abhishek Nov 11 '20 at 00:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224387/discussion-between-abhishek-and-eerorika). – Abhishek Nov 11 '20 at 00:13

4 Answers4

4

Can you use a class without objects?

Yes, you can. For example, you can access static members of a class.

I want to be able to use . when using static method

You cannot do what you want. The member access operator . can only be used on values of the class type and for that you need an object. The operator to access static member function of a class without an object is the scope resolution operator ::.

You also cannot call a non-static member function - such as A::Log of your example - without an instance regardless of syntax.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

You need to change your code as follows,

class A {
    static void log(const char* x) {
         std::cout << x << std::endl;
    }
}

//Call it as follows,
A::log("hello");

Observe the word, static. static methods are class level methods and should be called via class. You will not be able to call it with objects of the class A.

You can use struct instead of class if you like :-)

Abhishek
  • 6,912
  • 14
  • 59
  • 85
2

An alternative to using a class could be to use a namespace instead:

namespace A {
void log(const char* x) {
     std::cout << x << std::endl;
}
}

That way, your class doesn't need to exist, it can just be a free-function inside it's own namespace. You even call it the same way:

A::log("foo");
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
  • The changes are very small but I'm not looking for a namespace. A class(Because i want to use . and not :: small yes I know but thats what I'm looking for here) – Duffka Nov 10 '20 at 23:49
  • @Duffka If your function is static inside a class, and you don't have an object instance, you still have to use `::` with the class version too :-). But obviously if you want/have to use a class, then that's the way to go! – Karl Nicoll Nov 10 '20 at 23:53
0

But is there any possible way I dont have to make an object to use a class? I just want to be able to do A.log("Hello World!");

To use that syntax, you need an object.

With a small change, you can have your expected syntax:

/*inline*/ struct {
    void log(const char* x) {
         std::cout << x << std::endl;
    }
} A;

Demo

Anonymous class, with variable A of that type. If put in header included at several place, adding inline or static would avoid ODR-violation.

Using regular way (static method and A::log) is less surprising.

Jarod42
  • 203,559
  • 14
  • 181
  • 302