Ask coding questions

← Back to all posts
[Solved] C++ What is the point of referencing object methods via class
xxpertHacker (865)

One can call a method on an object, ex: "123"s.at(0), or reference the method from the class, ex: std::string::at.

Yet, the latter always causes an error, so how is it meant to be used?

Comments
hotnewtop
xxpertHacker (865)

@Coder100 Omg, all I needed to do was put the & before it...

&std::string::at;

Actually no, wtf is that?

Is there a name for this?

I found this syntax from C++ stdlib proposals and stuff.

Coder100 (17100)

are you sure you like that syntax lol @xxpertHacker

Coder100 (17100)

it 'sorta' makes sense but honestly...
static methods are better @xxpertHacker

xxpertHacker (865)

@Coder100 Oh, I love it.

Also, reading into that nonsense helped me understand C++ better (slightly).

If anything, it completely redefined the way that I thought of C++'s syntax. If you don't get what I'm saying, go look at the attached repl now.

I think all property access is merely implicitly based at the class, so var.foo is implicitly var.T::foo.

At the same time, this has to be the most complex property lookup syntax I've seen, to-date. Barely more complicated, but still.

And this allows one to easily bypass overridden methods/properties by referencing the superclass's property/method directly.

xxpertHacker (865)

@Coder100

it 'sorta' makes sense but honestly...
static methods are better

I don't think you understand... static methods can't replace this:

https://replit.com/@xxpertHacker/Dynamic-lookups

I was always lost as to why one couldn't assess arbitrary properties in objects, yet one could access arbitrary values in an array, when they are laid out in memory the same.

Coder100 (17100)

I think the latter was actually meant for definition, like:

... std::string::at(...) { ... }

ya know?

xxpertHacker (865)

@Coder100 Oh, lol, besides defining the methods.

I was hoping that the methods could just be passed around like a function reference.

auto const& func = std::string::at;

// use `func`
Coder100 (17100)

But it's method... u have to bind it lol @xxpertHacker

Baconman321 (1060)

@xxpertHacker Isn't "::" a scope resolution operator, meaning that it's meant to retrieve a value from a certain scope?

So, wouldn't it logically be used to retrieve something from that scope?

Except, it throws you an error.

Oh, wait I bet you already know that... -_-

Hmm, I think this would be a good read:
https://stackoverflow.com/questions/4984600/when-do-i-use-a-dot-arrow-or-double-colon-to-refer-to-members-of-a-class-in-c

Let me read it tho to see if it contains useful info...

I believe "::" is referencing something directly from a class, whereas "." is meant to reference something pointing to a reference to an object/namespace.

Coder100 (17100)

But as you can see my case is correct @Baconman321

Baconman321 (1060)

@Coder100 ?

It's used for definition?

Isn't binding where you create a address of an object?

Yeah, then that would make more sense as "::" is directly being used on the object/namespace.

Coder100 (17100)

it literally said to access it, never meant you made an address of it or anything, otherwise, how would you define a method?


and obviously, you can trust the autocomplete right

https://replit.com/@Coder100/LightblueWeakPixel-aoeu-aeoua-eoauaeo-ua#main.cpp
@Baconman321

Coder100 (17100)

basically from how i see it, he's doing something stupid and out of context

void main() {
  lmao_look_at_this_idiot;
}

void lmao_look_at_this_idiot() {
  // couldn't be me!
}

@Baconman321

Baconman321 (1060)

@Coder100
*looks at code*
Srsly?

Anyways, yeah whatever.

All I know is that "::" is meant to directly access an object/namespace.

Huh, wow that answered a few questions I had myself about C++.

Ahh, yes the power of researching someone else's question...

Coder100 (17100)

lmao
what were your questions? @Baconman321

Baconman321 (1060)

@Coder100 More or less his.

"Why use :: when you can do . ?"
"What is -> even used for anyways?"

Now I'm curious about std::operator but I can't find anything :(

Coder100 (17100)

std::operator?
that's just for overloading operators, allowing you to do the disgusting and useless and stupid and dumb and overall a dumb mistake

std::cout << "WHO" << "THE" << "FRICK" << "LOVES" << "THIS" << "GODDDDDD" << std::endl;

@Baconman321

Coder100 (17100)

ok i'm sorry but can i ask: have watched the most god C++ tutorial

probably won't be asking these questions smh @Baconman321

Baconman321 (1060)

@Coder100 True, but I don't usually don't go off of YT or some other video service.

I'd rather not re-learn C++ from the start.

I just need to know the in-betweens :/

Which, probably means I need to re-learn it sigh.

Eh, I find C much more attractive.

Altho, C++ is quite nice too.

xxpertHacker (865)

@Coder100

But it's method... u have to bind it

Okay, then how do I bind it? That is, without creating an instance of the class, and using property access notation?

Coder100 (17100)

@xxpertHacker I don't think that's possible with C++
that's like accessing variables inside a function

xxpertHacker (865)

@Coder100 Well, methods are functions, they're just called differently.

Consider how JavaScript allows you to directly access myClass.prototype.method, I'm trying to do myClass::method.

I'm looking around for that syntax I mentioned earlier: &myClass::method.

xxpertHacker (865)

@Coder100 Oh, I just remembered, you can call these methods in classes.

struct T {
	void foo() {}
};

struct H : private T {
	void bar() {
		T::foo();
	}
};

so the syntax has another use.

Coder100 (17100)

psst foo is private you can't do that @xxpertHacker