Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

pokeyman posted:

is the hint "car extends vehicle"

code:

type Car struct {
	*vehicle.Vehicle
}

Adbot
ADBOT LOVES YOU

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
go would be an absolute nightmare for anything involving business logic / business objects, imo

Luigi Thirty
Apr 30, 2006

Emergency confection port.

C++: I have an Widget class and a Button class that inherits Widget. Widget has an abstract draw method that returns a pointer to a char array, Button implements it. eventually I'm going to want to define more kinds of widgets than just Button.

I want to be able to fill an std::vector with a bunch of Widgets and call their draw methods without caring about their types. in c# I would use an IWidget interface and override the draw method in the child classes but I'm bad at c++. is there an equivalent I can do in C90/C++ 2.0/whatever this Watcom compiler understands or how should I be doing this?

GameCube
Nov 21, 2006

that's what virtual methods are

GameCube
Nov 21, 2006

class widget
{
public virtual char* draw() = 0
or maybe virtual public draw() = 0
i forget
}
class button
{
public virtual char* draw()
{
return &the_thing
}
}

there i wrote it for you

Soricidus
Oct 21, 2010
freedom-hating statist shill
don't mix virtual methods with stl containers unless you like being very careful about pointer ownership

or at least that's how things used to be, maybe it's better now that there are smart pointers in the stdlib

GameCube
Nov 21, 2006

Luigi Thirty posted:

C90/C++ 2.0/whatever this Watcom compiler understands

Soricidus
Oct 21, 2010
freedom-hating statist shill
lol :rip: then

brap
Aug 23, 2004

Grimey Drawer
lol at using haskell or go for anything

Luigi Thirty
Apr 30, 2006

Emergency confection port.

GameCube posted:

class widget
{
public virtual char* draw() = 0
or maybe virtual public draw() = 0
i forget
}
class button
{
public virtual char* draw()
{
return &the_thing
}
}

there i wrote it for you

oh I was trying to do that with a Button in a std::vector<Widget> and it was executing the parent class' draw method instead probably because I didn't declare the Button's draw method as virtual too

Soricidus posted:

don't mix virtual methods with stl containers unless you like being very careful about pointer ownership

or at least that's how things used to be, maybe it's better now that there are smart pointers in the stdlib

lol rip, back to arrays

Progressive JPEG
Feb 19, 2003

MALE SHOEGAZE posted:

go would be an absolute nightmare for anything involving business logic / business objects, imo

https://www.youtube.com/watch?v=nYscs78R5Fs

Progressive JPEG
Feb 19, 2003

Luigi Thirty posted:

oh I was trying to do that with a Button in a std::vector<Widget> and it was executing the parent class' draw method instead probably because I didn't declare the Button's draw method as virtual too


lol rip, back to arrays

if youve used java, you can think of it as 'java decided that methods should always be virtual'

Soricidus
Oct 21, 2010
freedom-hating statist shill
basically if you're using classes with inheritance in c++ then you want to always use pointers, partly because that's the way you make virtual methods actually be virtual, and partly because horrible things happen if you try to put a Button in a collection (or array) of Widgets when sizeof Button != sizeof Widget

netcat
Apr 29, 2008

Luigi Thirty posted:

oh I was trying to do that with a Button in a std::vector<Widget> and it was executing the parent class' draw method instead probably because I didn't declare the Button's draw method as virtual too


lol rip, back to arrays

there's no reason you can't put a class with virtual functions in an stl container wtf

fritz
Jul 26, 2003

Soricidus posted:

don't mix virtual methods with stl containers unless you like being very careful about pointer ownership

or at least that's how things used to be, maybe it's better now that there are smart pointers in the stdlib

wait why not

i know youre not supposed to subclass containers b/c of virtual dtors but beyond that ???

Progressive JPEG
Feb 19, 2003

always virtual your destructors

Illusive Fuck Man
Jul 5, 2004
RIP John McCain feel better xoxo 💋 🙏
Taco Defender

Luigi Thirty posted:

oh I was trying to do that with a Button in a std::vector<Widget> and it was executing the parent class' draw method instead probably because I didn't declare the Button's draw method as virtual too


lol rip, back to arrays

people have kinda already said this, but you need to use std::vector<std::unique_ptr<Widget>>, or std::vector<Widget*> (if something else has ownership). If you assign a Button to a Widget (not pointers), the button stuff will get sliced off.

there's nothing wrong with using stl vectors for this. if std::unique_ptr isn't available you should probably write your own. a basic implementation shouldn't be complicated.

Luigi Thirty
Apr 30, 2006

Emergency confection port.

looks like I have auto_ptr but unique_ptr was added in C++11

JawnV6
Jul 4, 2004

So hot ...
why would you NOT want pointers in a container

Brain Candy
May 18, 2006

Luigi Thirty posted:

looks like I have auto_ptr but unique_ptr was added in C++11

no. stop. don't. even in the special hell you are stuck in you can use boost

Progressive JPEG
Feb 19, 2003

btw if you're going to use boost anyway, it's got ptr_vector

JawnV6
Jul 4, 2004

So hot ...

Brain Candy posted:

no. stop. don't. even in the special hell you are stuck in you can use boost

now u have 2 problem

VikingofRock
Aug 24, 2008




What's wrong with boost?

JawnV6
Jul 4, 2004

So hot ...
like every other part of c++, I used it 10 years ago

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

MALE SHOEGAZE posted:

code:

type Car struct {
	*vehicle.Vehicle
}

this is called a tagged gounion

Zemyla
Aug 6, 2008

I'll take her off your hands. Pleasure doing business with you!

pokeyman posted:

this is called a tagged gounion

Pronounced "gown-yawn".

anthonypants
May 6, 2007

by Nyc_Tattoo
Dinosaur Gum

Zemyla posted:

Pronounced "gown-yawn".
gee-onion

Luigi Thirty
Apr 30, 2006

Emergency confection port.

pronounced "bunion"

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Luigi Thirty posted:

I want to be able to fill an std::vector with a bunch of Widgets and call their draw methods without caring about their types. in c# I would use an IWidget interface and override the draw method in the child classes but I'm bad at c++. is there an equivalent I can do in C90/C++ 2.0/whatever this Watcom compiler understands or how should I be doing this?

as long as you make it a std::vector<Widget *> (or Widget&, or a smart pointer to a Widget) it should work fine since you're passing around pointers and can make Widget::draw() virtual

just be sure you have well-established ownership rules for your objects, for example in Cocoa views are owned by their superviews, so you need to take ownership before removing a view from its superview if it needs to stick around

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Soricidus posted:

basically if you're using classes with inheritance in c++ then you want to always use pointers, partly because that's the way you make virtual methods actually be virtual, and partly because horrible things happen if you try to put a Button in a collection (or array) of Widgets when sizeof Button != sizeof Widget

and all that's because Stepanov said software must be written from the algorithm outward, and decided to enforce that with the utter poo poo design of the C++ Standard Template Library for any real-world development

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
christ, what an rear end in a top hat

brap
Aug 23, 2004

Grimey Drawer
what does it mean to design from the algorithm outward

ErIog
Jul 11, 2001

:nsacloud:

Soricidus posted:

basically if you're using classes with inheritance in c++ then you want to...

drink a lot of whisky to deal with the pain of both C++ and inheritance

JimboMaloi
Oct 10, 2007


the stallman-approved reimplementation of Tor

FamDav
Mar 29, 2008
phone posting but jdk 9 will have standalone modules that include everything you need to run

tef
May 30, 2004

-> some l-system crap ->
go as imagined:

imagine C
but we got rid of malloc and free
we got rid of include files
and Makefiles
we added a built in list, hash table, and unicode string type
we added tools to auto-format your code, as well as fix old syntax
we didn't get to exceptions, or generics, but they're hard to do and we're used to life without them
but we did like pipes and processes in unix so we added a lightweight system to run little snippets of code which can use pipes
oh and cross platform builds,

it doesn't sound that awful compared to C

(i mean golang only looks weird when you ignore all the other things like limbo or alef or plan-9 c, it's kinda incremental)

go in reality:

what do you mean you wanted a repeatable build or stable dependencies

*slaps github urls everywhere imports before making GBS threads pants*

JimboMaloi
Oct 10, 2007

FamDav posted:

phone posting but jdk 9 will have standalone modules that include everything you need to run

im embarrassingly excited for java 9

not that theres a level of excitement that wouldnt be embarassing

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

FamDav posted:

phone posting but jdk 9 will have standalone modules that include everything you need to run

this is cool


tef posted:

go as imagined:

imagine C
but we got rid of malloc and free
we got rid of include files
and Makefiles
we added a built in list, hash table, and unicode string type
we added tools to auto-format your code, as well as fix old syntax
we didn't get to exceptions, or generics, but they're hard to do and we're used to life without them
but we did like pipes and processes in unix so we added a lightweight system to run little snippets of code which can use pipes
oh and cross platform builds,

it doesn't sound that awful compared to C

(i mean golang only looks weird when you ignore all the other things like limbo or alef or plan-9 c, it's kinda incremental)

go in reality:

what do you mean you wanted a repeatable build or stable dependencies

*slaps github urls everywhere imports before making GBS threads pants*

this is true

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison
on the other hand it's also not really an insurmountable problem in the short term - fork your dependencies then pull from master when something comes up that you need.

Adbot
ADBOT LOVES YOU

cinci zoo sniper
Mar 15, 2013




St Evan Echoes posted:

its the whipcrack of management's latest vim

  • Locked thread