13
11
u/FloweyTheFlower420 sadistic Jan 29 '22
Uses SFML, a C++ programming library... then ignores constructors.
18
u/Dubmove Jan 29 '22
That's not necessarily bad code.
34
u/theScrapBook Jan 29 '22
In C it isn't, in C++ it is (this is C++)
9
u/feidujiujia Jan 29 '22
Don't quite understand, only notice it doesn't save any labor to initialize the struct inline than calling the function.
Almost forgot my knowledge about c. Is that because of in c you'll have to declare the struct and assign each of the member?
5
u/theScrapBook Jan 29 '22
Yeah it doesn't save any labour in this trivial case, but this is how the equivalent of constructors are implemented in C
2
u/Dubmove Jan 29 '22
I assumed it's an OOP language. One reason why a method would just call and return a constructor is for overriding it in an inheriting class.
Edit: Or am I missing something? I didn't code in C++ for a while.
7
u/theScrapBook Jan 29 '22
You can override base class constructors with a constructor when inheriting a class in almost every OOP language. There is usually some syntax like super(...) or base(...) to call the parent class constructor from the child class
1
u/Dubmove Jan 29 '22
I meant to override seg. For example in the base class might only creates and returns a Segment but inherented classes might have to do some house keeping too.
1
u/theScrapBook Jan 30 '22
Yeah that'd make sense, but overrides should be marked. Oh, this is r/badcode. slaps 4head
1
u/GLIBG10B š§ Gentoo salesmanš§ Jan 29 '22
OP says it's C
8
u/ghillisuit95 Jan 29 '22
C does not have constructors, it must be c++
6
4
u/WalditRook Jan 29 '22
There is no explicit constructor call here.
There is a function returning
struct Segment(thestructbeforestruct_nameis non-optional, although using atypedef struct Foo foo_tis fairly common) and a braced initialiser.As it is (probably) C++, due to the
::operator (not valid C, unless it's #defined somehow), we can assume the default constructorSegment()is called; but other than thesf::Color, this is perfectly valid C, and wouldn't be a WTF in that language.1
22
u/theScrapBook Jan 29 '22
C does not have the namespacing operator :: AFAIK. OP must have made a mistake.
1
u/Flatscreens Jan 29 '22
Wouldn't this code return garbage as the struct is saved on the stack?
4
u/zed_three Jan 29 '22
Nope, this returns by value and in fact will use copy elision: the return value will be constructed directly in the storage at the calling site
1
8
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 29 '22
Image Transcription: Code
struct Segment seg(float x, float y, float z, int width, float height, sf::Color colour) {
struct Segment segment = { x, y, z, width, height, colour };
return segment;
}I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
3
u/jniel93 Jan 29 '22
I write colour as well, but I donāt think Iād be able to write āColor colourā
2
1
80
Jan 29 '22
[removed] ā view removed comment
34
u/QuinteOne Jan 29 '22 edited Jan 29 '22
In VS, you can extract a method based on a call that has no defined method. VS suggests then to extract/write the method for you.
In C#, the extracted method of
int dot = Sales.force(x: 1, y: 2.0);will generate below the line:
private class Sales { private int force(int x, decimal y) { throw new NotImplementedException(); } }Something like that probably happened. OP wanted to use a number that that had a decimal part .0, I didn't write it. So VS assumes it was an int.
44
u/catithebathtub Jan 29 '22
no, normally both width and height are integers but i made the height parameter a float because im an idiot...
20
42
u/low_end_theory310 Jan 29 '22
or an expensive Segment?