« first day (375 days earlier)      last day (4577 days later) » 

9:00 AM
@RMartinhoFernandes I don't really understand that. Say my IP address is 192.168.1.1 and 192.168.1.2 my server listens to incoming packets with both addresses.
 
Damn, I forgot all my network programming... in the function where you establish the connection you can tell it to use a particular ip or port, but it defaults to "I don't mind, choose one yourself"
 
The client wants to connect, I don't use any of the above, but I use 127.0.0.1 :S
 
@LewsTherin Those addresses are valid within your local network.
Within your machine 127.0.0.1 is another valid address.
You can use any of those from your machine.
 
@DavidRodríguezdribeas I don't know..how do you do that? I was able to fill only the server addresses
@RMartinhoFernandes So 127.0.0.1 is the address of the actual machine, but 192.. is the address of the router
 
No, no.
127.0.0.1 is always the address of the machine you are on.
But from the outside, you can't access that machine with 127.0.0.1.
Your machine has an address that is usable within the local network (192.168.x.x and some other addresses are reserved for this purpose).
 
9:04 AM
But If I say the server address is 127.0.0.1 is the address I want to connect to, all machines have the localhost address, how would it differentiate which?
 
@LewsTherin You can't use any 127.x.x.x address remotely.
Those are always local addresses.
 
127.0.0.1 (or any other local address) never leaves your own computer
it does not even make it to the network card
 
So, if you are on machine A, 127.0.0.1 refers to machine A. On machine B 127.0.0.1 refers to machine B.
 
@RMartinhoFernandes Does that mean it doesn't go through the router then?
 
9:06 AM
So it would work even if I disconnected from the internet
 
Yes.
Like David says, it doesn't even need a network card.
Your router will have an internal address of the form 192.168.x.x (or one of the other local network reserved addresses) and an external address.
The outside world only sees the external address.
 
Ugh, that programming example was bullshit then. I need to try the proper one. Ah, thanks for clearing that up
 
But from within you can use 192.168.x.x to refer to it.
 
Um so, how would I find out which Ip address to use? If it uses INADDR_ANY
I tried using that for my client but it didn't work
 
You can't connect to anything.
You need something specific to connect.
(Though there are protocols for server discovery, but those are not relevant here.)
 
9:09 AM
But if the server uses any Ip address, I wouldn't know which address to connect to...
 
The server is just listening.
The "any" on that means "any of the addresses this machine is known as".
That means you can use 127.0.0.1, or 192.168.x.x to connect to it.
 
Yeah, but the server has an Ip address and port number I must connect to. the problem is I don't know the ip address of the server. INADDR_ANY let's it listen to all ip addresses that matches its host, right?
oh
 
Right.
You can run ipconfig on the command-line on Windows, or ifconfig on Linux to see your IP addresses.
 
But I'm confused if 192.168.1.1 is my ip address isn't that common for most routers?
oh, why the change? fml
 
@LewsTherin ifconfig.
@LewsTherin That's a local network address. It can't be used from the outside. So it only causes trouble if you have two machines in the local network with that address.
My router is at 192.168.2.1 (local) and at 231.22.4.211 (external).
(That's not really my external IP, I made it up just to get the idea across :)
 
9:15 AM
@RMartinhoFernandes lol. So when someone external tries to connect with 192.168.1.1 they will only be connection to their router assuming their lan as that address. ifconfig didn't show my external address however
 
I can use the first one to connect to it, but you can't.
@LewsTherin You need to be outside to see it: whatsmyip.org will show it.
 
@RMartinhoFernandes You can now google 'what is my ip' for the same result!
 
@LucDanton Oh, neat.
 
@RMartinhoFernandes Why do I have to be outside?
 
@LewsTherin Because the router associates that IP with its (physical) Internet port.
 
9:18 AM
The router treats my real address as the router address..?
 
What do you mean by "router address"?
 
The address of the router 192...
 
The router has two addresses.
 
Yeah
 
One for local stuff, another for remote stuff.
 
9:21 AM
Oh so 192. is the local address I am using, and 244.132.1... is the remote address?
 
The router bridges between two networks, and has an address for each.
 
@LewsTherin Right.
 
@RMartinhoFernandes But if 2 machines connect to the same router, would they share the same local ip address?
 
On the local network?
Each machine has its own.
Link-local IPv6 Address . . . . . : fe80::89f5:19f:5527:a57c%11
IPv4 Address. . . . . . . . . . . : 192.168.2.104
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.2.1
This is what the relevant output of ipconfig looks like on my machine.
The address of the router is the last one (ending in 1).
 
Yeah, so every machine in your house would use 192.168.2.1
 
9:23 AM
My machine has a different address, the one ending in 104.
@LewsTherin Only the router has that one. You can't have two machines on the same network with the same address.
But every machine here connects uses 192.168.2.1 to connect to the router.
 
So what is 192.168.2.104 address of what?
 
@LewsTherin My machine.
 
@RMartinhoFernandes :S Is that an alias for 127.0.0.1?
Ha ha I'm not getting this at all Smh
 
@LewsTherin Not exactly. It is the address associated with my Wi-Fi card, while 127.0.0.1 is not associated with any.
But I can use either to connect to a server on my machine (provided it is listening on both addresses, obviously).
 
oh, so my laptop has its own address 127.0.0.1.
The NIC has its own address 192.168.2.104 and the router has its own addresses, the local and remote one.
 
9:27 AM
You can think of the computer as being on two networks, the one being the local network with the router, and the other being a 'special' network with just the computer on it.
 
@LewsTherin What's a NIC?
 
@RMartinhoFernandes Network Interface Card I think
 
yeah
 
Ah, right.
@LewsTherin You're correct then.
If I were to plug a cable to the router I would get yet another address, associated with the cable network card.
 
@RMartinhoFernandes Does that mean that when if I connect with 127.0.0.1 the request never makes it to the NIC. But if I connect with 192.168.2.104 it reaches the NIC but jumps back? Never leaving the computer?
And if I connect with 192.168.2.1 it reaches the NIC, Router and jumps back, never leaving the router? lmao
 
9:30 AM
@LewsTherin The first one, yes, you're correct. You don't even need a card. The second one, I don't know. I think it depends on the OS.
@LewsTherin If I connect to 192.168.2.1, I connect directly to the router. If the router provides a service on the port I used, I can use that.
My router provides a web interface for configuration on port 80 for example (I think this is common).
 
@RMartinhoFernandes What does that mean? That the router listens on a port as well?
 
if you connect to an address that one of your local NICs is using no modern OS will let that get anywhere near as afar as the NIC itself
 
@awoodland Because there is no point?
 
@LewsTherin Yes, the router has an HTTP server listening on port 80.
If I use any other port, I'll just get a failed connection.
 
@RMartinhoFernandes Um , what if the server wasn't listening at port 80?
 
9:34 AM
@LewsTherin - sending it over the PCI bus or whatever physically connects the NIC to the host CPU twice would be crazy
 
@awoodland How do you mean twice?
 
@LewsTherin In that case there would be nothing to connect to, so any connection attempt would be futile.
 
TX over bus once and then RX back from the card = twice
 
0
Q: How to do refactoring this structure, if-else-if-else-if * 100

BenjaminThere is some nasty legacy code. std::string xxx = GetCommand(); // get "CommandX"; if (xxx == "Command1") { return new Command1(); } else if (xxx == "Command2") { return new Command2(); } ... else if (xxx == "Command100") { return new Command100(); } I want to improve this code ...

Poor sod.
 
@RMartinhoFernandes That doesn't make sense. I have specified that the server listens at this port and my client knows which port, but the router would block it anyways? So over all it is better to use external ip address
 
9:36 AM
I thought code like that was only created for TDWTF
 
@awoodland I guess so, but it has no choice using external addresses
 
@LewsTherin If you connect to 192.168.2.1, you're trying to connect to the router. It won't make a difference if there's anything listening on some other machine.
 
@LewsTherin - sure it does, the host CPU controls what the NIC thinks of as "my address"
(actually the NIC only thinks in terms of MAC addresses anyway)
 
@RMartinhoFernandes What the... It thinks my server is at the router?
 
@LewsTherin If you connect to the router's address, yes.
 
9:38 AM
(the NIC knows nothing about IP or anything higher layer)
 
If you want to connect to some other machine, you need to use that machine's address.
 
(it might just about know about checksum algorithms if you have a fancy expensive one instead of a $2 cheap one)
 
@RMartinhoFernandes What if I use the remote address then? Would it still get blocked?
 
@LewsTherin You mean the external address on the router?
 
@awoodland Ah jeez, going too complex.
@RMartinhoFernandes Yeah
 
9:39 AM
It probably won't work. At that point NAT and shit gets involved and things get a bit messier. Best not go there right now.
 
@RMartinhoFernandes What's this NAT? Why wouldn't it work? I mean I am saying connect to this machine here is the address the server is listening to, the external ip on the router and the port number.
 
@LewsTherin But that's an IP for the router, not your machine.
 
@RMartinhoFernandes I thought the external address was used for other machines to connect to my machine.
 
NAT is Network Address Translation. It's a gimmick to allow you to access the outside through the router, with the outside only seeing the router.
@LewsTherin Well, yes, but that only happens with NAT.
 
@RMartinhoFernandes So without this NAT stuff, my connection wouldn't work in theory.
 
9:43 AM
Right.
 
@LewsTherin - without NAT you'd have to have real routable addresses everywhere like the internet was design :)
 
@awoodland We'd have ran out of IPv4 space decades ago!
 
@RMartinhoFernandes So connecting to a server using the router address and router external address is pointless. They are almost the same thing
 
@LewsTherin Unless the server is running on the router, yes, it's pointless.
 
@RMartinhoFernandes - I always wonder if the engineer/developer time making every single higher level protocol work around NAT would have been more or less than the cost of rolling out IPv6 earlier
 
9:45 AM
@RMartinhoFernandes Is that what's happening ? Server running on routers? How does one do that :S
 
@LewsTherin Type your router address on your browser's address bar.
 
@RMartinhoFernandes I did it asked me for the password as if I was using the 192.168 address
 
@LewsTherin See, there's an HTTP server running on your router.
You just connected to it.
 
@RMartinhoFernandes So when I'm connecting to google say 232.120.23.32 I'm actually connecting to a server on the router?
 
No, you're connecting to a server on the machine that has that address.
 
9:49 AM
@RMartinhoFernandes But that machine is connected to a router...
 
@LewsTherin Yeah, things like NAT magic that detail away.
 
@RMartinhoFernandes But if there was no NAT, it wouldn't work...
Sorry for repeating this questions. It's just a way to understand..
 
@LewsTherin Without things like NAT every machine would have an external IP address.
 
@RMartinhoFernandes That sounds as if NAT is giving the machine external IP addresses, I thought the routers already did that
 
NAT lets lots of devices share one public IP address
so to the outside world it looks like only one device (address) is making connections to services
 
9:53 AM
Oh, so without NAT each machine must have their own external Ip address? But with NAT they can all share the one the router provides
 
yeah
NAT is the fudge that makes that magic work
 
Oh I think I understand that. But what if the 2 machines are connected to this one router, so they share 232.120.23.32 and some bloke from the other end tries to connect to that address, how would it know which machine to talk to? It uses the port number I assume?
 
Yes, NAT rearranges the ports.
 
How do you mean rearranges?
 
9:56 AM
@LewsTherin - if that is part of an existing connection the NAT device remembers who the real end point for the conversation was and forwards it accordingly. If it's a new connection then it looks at some rules to decide who should handle it
 
@awoodland Yeah, I mean new connection. So NAT is stateful?
 
Yes, the router keeps a map from (local machine ip, port) to a router port.
 
@LewsTherin - yup, that's the T in NAT - table
(of connection state)
 
@awoodland I think it's "Translation".
 
@RMartinhoFernandes But the port number of the machine is different to the one the router listens to...
 
9:58 AM
@LewsTherin Yes. That's why it needs a table.
It translates accordingly.
 
@awoodland "What's the T"?
 
@LewsTherin - it is Translation
but normally you'd do it with a table :)
 
If you send a packet from your machine (192.168.1.100) from port 1234, let's say the router could associate that with it's own port 4321.
The destination machine the outside will see that packet coming from the router (192.168.1.1) from port 4321.
It doesn't even know your machine exists.
 
on the LAN side if you have N clients then you have N * 65536 ports, which you have to map onto 65536 ports for the one public address
in practice you're unlikely to get anywhere near all N clients wanting all 65536 ports simultaneously
 
If a packet comes to the router (192.168.1.1) to port 4321 (which is where the outside machines will send them too, since that's the endpoint they see), it will see in the table that that port corresponds to port 1234 on your machine (192.168.1.100), and send you the packet accordingly.
 
10:00 AM
so you pick a spare one
 
Hold on.....
 
and avoid conflicts by altering them as the pass across the NAT device and (hopefully) nobody realises this is happening
 
Ah yes you corrected it lol
Oh no you didn't hold on
 
I think it's correct.
Ah, the router address in that example should be the external one :) Sorry about that.
 
"it will see in the table that that port corresponds to port 1234 on your machine (192.168.1.100), and send you the packet accordingly."
It shouldn't care about the port 1234 on my machine.
It should try to say oh is there a server with a port 1234 connected to the router?
 
10:05 AM
@LewsTherin No, because you're not connected to the router.
Your connected to some machine outside.
The router is in the middle NATing things, but you're not connected to it.
It just happens that the packets you send out will pass through the router, and it will "forge" the address and port information on those packets before passing them out.
 
I will try again, I am trying to connect to google. I assume the machine at google is connected to a router. I make a request to that server. The router sees the packet with source ip address and port 192.168.1.1:1001
and a destination ip address and port 192.168.1.1:1234?
But the router at google listens to 4321
Do you get my point? lol
 
outside of home networks NAT is comparatively rare
 
Ok, let me go over it again :)
 
well home + mobile phones
 
You're connecting to Google, which is at 209.85.147.104:80.
Your browser opens a socket and it is bound to port 1234, and your address 192.168.1.100.
 
10:09 AM
Those are the clients ?
obviously lol
 
Then it sends TCP packets to establish a connection, and these will have 192.168.1.100:1234 marked as the source in the headers.
 
yeah, so far so good
 
Because that's a local network address it can't be used from the outside.
The router (external address 222.222.222.222) will see those packets and will pick one of its own ports that is free. Let's say it picks 4321.
It will modify the packet headers and they will now have the source as 222.222.222.222:4321.
It passes them out.
 
Ah sorry, I understand. I was thinking of the servers'
 
These packets reach the server at Google, and that server sends a reply saying the connection was accepted.
 
10:12 AM
Which means routers are computers -- you can use them as your personal mainframes!
 
@LucDanton NOOOOO!
Use printers for that.
@LewsTherin Ok, so I don't need to go on then?
 
use the network for storage (how many echo requests can you have in transit?)
 
@RMartinhoFernandes If there is more, please do :)
 
That TDWTF had me secretly wanting to learn PostScript.
 
I've written some postscript by hand, but it's something I aim to do less of
 
10:14 AM
Ok. Google will send the reply back to the source. As far as that server knows, the source is 222.222.222.222:4321, so that's where it sends the reply to.
 
gl2ps is an amazing library for generating decent EPS and SVG figures from an API you already know :)
 
That means it goes straight back to the router. The router sees this packet arrive on port 4321, which is a NAT port. It looks it up in the table, and sees it is mapped to your machine 192.168.1.100, port 1234.
 
@RMartinhoFernandes Does the router then translate 222.222.222.2222:4321 to 192.168.100:1234 or does it only translate the port number? I don't see why it should translate the external ip
 
@LewsTherin It rewrites both the address and the port before forwarding it to your machine.
Your machine will simply drop any packets not addressed to it.
 
@RMartinhoFernandes Oh why'd it need to rewrite the external address to the local address? I guess different numbers?
 
10:17 AM
NAT has to make everybody believe it doesn't exist
 
@LewsTherin Because the external address is not one of your machine's addresses. It's the router's address.
 
@RMartinhoFernandes Yeah, as is 192.168.1.100
 
@LewsTherin 192.168.1.100 is your machine's address.
(That's what I said in the beginning, modulo any typos.)
 
Looks like scary networking stuff lol
 
@RMartinhoFernandes Oh right...using 192.168.1.100 confused me there... what if I had used the local router address?
@MrAnubis Tell me man!
Would the router translate it again to 222.222.222
 
10:20 AM
@LewsTherin How so? You can't use the router's local address as your own.
The router's local address is to allow local machines to connect to it (to access a configuration interface for example).
 
@RMartinhoFernandes right.. which means the local address of the NIC must be different from that of the router
 
@LewsTherin i can't say , forgot all theory 0_o , exam days?
 
Yes, you can't have two machines with the same address on the same network.
 
@MrAnubis lmfao it's too complicated but it is interesting
@RMartinhoFernandes The machine address is similar to that of the router..wish they didn't do that
 
@LewsTherin It's similar because it's on the same network.
(And because both are part of a range reserved for local addresses.)
 
10:25 AM
@RMartinhoFernandes the local machine address is basically that for the ethernet cable or wifi card or something.
 
@RMartinhoFernandes - Gang of Four bingo!
 
@LewsTherin ARP
 
@LewsTherin That address is not set by the card. It is set by the OS, either manually, or through some network configuration protocol like DHCP.
Yes, it's protocols all the way down.
 
@RMartinhoFernandes Oh right, makes sense I guess. Omfg head implodes
 
Sweet, it's raining.
 
10:29 AM
I'm still trying to understand why I can't use the router's local address. OMFG.
Would the router open up a webpage or something lol
 
@LewsTherin Think of the router as another computer.
(In fact, it could actually be just a regular computer like yours.)
You can't use its address because it's not yours.
Say, I live at Some Street, 18, Some City, and you live at Some Street, 20, Some City. Why can't you use my address?
 
Ah yeah. Good example. If I did it that way, all letters I send using Some Street, 20 would arrive at that address. Not my address
Which means the router still allows the translation, but the response stops there...at the router.
 
Right. (Now don't go stretching that metaphor. It breaks apart easily.)
 
Public IP is like a street address. Private IP is like your room number in a house that is shared by multiple tenants. Port forwarding is like forwarding of mail packets from the home owner to the tenant.
 
Public Ip is that the local or external?
 
10:35 AM
External. With private IP I meant local IP.
 
this code is valid?
 
	struct X {
		int foo(int);
	};
	function<int (X*, int)> f;
	f = &X::foo;		// pointer to member
	X x;
	int v = f(&x, 5);
 
An empty source file gives no compiler errors. So it's valid.
Damn, too late.
 
10:37 AM
@StackedCrooked I was going out on a limb.
There are more possible invalid pieces of code than valid.
 
@RMartinhoFernandes @awoodland @StackedCrooked thanks a lot guys. I have some other question , but no time now. When I get back. But I got the idea thanks
 
@MrAnubis I always use boost::bind. I don't know if it is valid.
 
@MrAnubis Looks valid to me. Assume you're using std::function.
 
It's not. Need something to adapt the pointer to member, like std::bind.
 
@RMartinhoFernandes this binding is hard to understand for me -> f = &X::foo; // f can hold function of signature int (X*, int) but X::foo has completely different signature
 
10:39 AM
Reason being, you can't call with a ptmf: p(object, arg0, ...)
 
@LucDanton Oh, that's what mem_fn is for then?
 
@RMartinhoFernandes No, std::bind :)
 
WTF is mem_fn for then?
Seems important, otherwise they wouldn't pollute the namespace with a name similar to mem_fun.
 
// Usually I use function objects like this:
typedef function<int(int)> Foo;
Foo foo = bind(&X::foo, &x, _1);
foo(5);
 
> The adaptors ptr_fun, mem_fun, mem_fun_ref, and their corresponding return types are deprecated.
 
sbi
10:42 AM
@RMartinhoFernandes Isn't this from C++98? That is, from before bind() was introduced to the standard.
@LucDanton Ah, indeed, then.
 
@sbi mem_fun is deprecated, but mem_fn is new.
 
Short version: those and the old binders were the beta versions of std::bind. std::bind alone supersedes them all.
 
See, it causes confusion. Why add it then?
 
I think that unlike std::bind it doesn't require using placeholders.
 
@RMartinhoFernandes can you reply to my last reply?
 
sbi
10:43 AM
@RMartinhoFernandes I thought the whole point of C++ was to cause utter confusion? :) (Is there any other reason to consider it a success?)
2
 
Also, I prefer mem_fun.
Anything with "fun" in the name, gets my immediate approval.
3
 
morning
 
@MrAnubis Since it's a member function it has a hidden parameter: this.
 
sbi
@TonyTheLion It's almost 1pm, you know.
 
@sbi it's 11:45 where I am
 
10:45 AM
He could be in the UK.
 
@RMartinhoFernandes Longer version: mem_fn supersedes the old *_fun wrappers, and std::bind the old binders, but std::bind can also do the job of mem_fn, although more verbosely.
Let me check if that's strictly true though.
 
@TonyTheLion UK again?
 
sbi
@TonyTheLion Ah, I didn't know. Job hunting?
 
10:46 AM
yes
 
sbi
@TonyTheLion You are tight-lipped. Is it that kind of job?
 
What?
I guess I don't want to know, right?
 
Yes, I can confirm that std::bind does overlap with mem_fn.
 
@sbi I'm looking for a job
there isn't much else to say about it really
 
Have you tried job fishing instead?
 
10:48 AM
@RMartinhoFernandes Does my explanation make sense to you?
 
@LucDanton Yes.
 
@RMartinhoFernandes what?
 
Nevermind. Lame joke.
 
sbi
@TonyTheLion What he meant: Instead of job hunting you could try job fishing. I suppose it is an option. I myself would vastly prefer job picking, though.
 
@RMartinhoFernandes one more question before I leave lol. Is it possible that 2 machine ip addresses can be the same? If the OS does all that stuff, isn't it remotely possible?
 
10:57 AM
@sbi oh I see
 
Yes, it's possible. Just manually set your machine's IP address to the same as another machine in your network.
Don't expect things to work though.
 
@RMartinhoFernandes It should work if the port numbers are different, if the ip address is translated first then it tries to find a matching port number...
 
@LewsTherin No, really, it's better to just treat it as undefined behaviour.
There are many other things in play here.
MAC addresses, the ARP cache...
 
@RMartinhoFernandes oh I suppose because the computer address to can have the same port number
MAC addresses is only unique to the router though
 
@LewsTherin It's unique per network interface.
@LewsTherin The main problem is what machine do the other machines think is the "owner" of that address.
 
11:01 AM
@RMartinhoFernandes the other machines, being the router?
 
For example.
 
(Windows will actually spot another machine responding to ARP requests for an address it thinks it owns)
 
I was thinking if it uses ip address as a key it could look for all the related index and check the port number or something
 
Each machine keeps a map between MAC and IP addresses (the ARP cache). Having two machines with the same IP will mess with this.
 
@LewsTherin - unless you have the "site-local" bit set then all unicast MAC addresses are globally unique
 
11:05 AM
Really, don't do it.
 
mmn.. yeah why would the machine keep a map? It is the router that does the work.. did you mean router? I won't do it, lol.
 
The packets also need to be routed down at the Ethernet level. That's done using MAC addresses.
 
you'll have machine<->machine traffic
e.g. file sharing
local service lookup
that will cause ARP caches on other machines to know about each other
 
Oh right, is that tcp?
ftp and all that
@RMartinhoFernandes mac address is different from the machines ip address though
 
@LewsTherin That's why a map is needed!
 
11:11 AM
Oh jeez, so it uses the computer ip address and mac address as well?
to decide which Ethernet cable the packet should be transferred to.... But if mac address is unique across each cable. A shared ip address should be ok...
I have it in my head as if (1234==addr["192.168.1.1"] && macAddr==something) some stupid shit like that lol
 
No, it won't be ok.
Some machines will have that address associated with machine A, and others will associate it with machine B.
 
If you mean the ip address yeah, both machines can have the same ip address. But the router can make a decision based not only on the ip address and port number but using the mac address, Which is definitely unique
 
router's don't use MAC addresses to decide though
 
What? How come?
 
they work on the layer above that
switches switch based on MAC addresses
routers route based in IP addresses
 
11:15 AM
@LewsTherin No, they can't have the same IP address.
 
Oh I thought Routers used the MAC, what uses that then?
when we talk of routers isn't that the data link layer
 
switches and the actual low level packet transmission
MAC address and ethernet frames are layer 2
 
Damn, I really have to go now. Thanks, talk later :)
 
IP is layer 3
 
11:42 AM
How do you do this:
public class Zoo {

private Animal animal;

public void someAction() {

for (int i = 0; i < 10; i++) {

switch (i) {
case 1: animal = new Tiger(); break;
case 2: animal = new Cat(); break;
case 3: animal = new Dog(); break;

...
}

}
}
 
What's the problem?
 
in c++, I want the object I create in the switch to be available as long as the animal variable isn't assigned a new object
 
Ok, there's some memory management issues here.
 
cpx
@BPDeveloper is this C++?
 
Disclaimer: This is not real code, just concept
 
11:44 AM
(btw, why are you using a for with a switch inside?)
 
no Java
I can't understand how you do this in C++
and this is just code that shows the concept of what I don't understand
I am not planning to start a Zoo :=)
 
for(i = 0; i < 10; ++i) {switch(i) { case 0: blah0; break; case 1: blah1; break; case 2: blah2; break; ... } } is the same as blah1; blah2; .... No loops, no switches.
 
I am thinking of the creation of objects and polymorphi
 
You need to come up with a better example.
 
The animal is a member variable that can hold different animals
 
11:47 AM
haha are you guys still messing with the zoo class?
 
@BPDeveloper Right, but what's the loop for?
 
skip the loop part then.... how would you create an object so that a member variable can hold it?
 
Use a std::unique_ptr.
 
Not learned that, isn't there other options*?
 
If you can't use smart pointers, you have to use a pointer and manage the memory manually :(
 
11:50 AM
So how would you do that?
 
You know about destructors?
 
Yes, we had a little bit of them
 
Newcomb's paradox, also referred to as Newcomb's problem, is a thought experiment involving a game between two players, one of whom purports to be able to predict the future. Whether the problem is actually a paradox is disputed. Newcomb's paradox was created by William Newcomb of the University of California's Lawrence Livermore Laboratory. However, it was first analyzed and was published in a philosophy paper spread to the philosophical community by Robert Nozick in 1969, and appeared in Martin Gardner's Scientific American column in 1974. Today it is a much debated problem in the phil...
 
Ok. You allocate an appropriate object with new in the constructor, and deallocate it with delete in the destructor.
But there are a lot of things to take care of to make this work correctly (That's why I recommended the smart pointers.).
 
But how would you do it if the method that assigns the variable may be invoked many times?
 
11:52 AM
First off, you need to make the destructor of Animal virtual.
 
You need to apply just the right amount of bananas.
 
do you write delete animal; then assign to the new value
 
Go ahead and do it now.
Then you need to take care of the copy constructor and the assignment operator of the Zoo class.
@BPDeveloper That would not work well in the presence of exceptions, but it's a starting point.
 
and what is this smart pointers?
 
unique_ptr is a smart pointer class. It handles all this mess transparently for you with practically no extra effort.
 
11:58 AM
And what would happen again if you return Dog dog; from a function?
 
user34537
@BPDeveloper I like to say if you use delete you are 'doing it wrong'
 
@acidzombie24 Probably, because I don't understand a thing
 

« first day (375 days earlier)      last day (4577 days later) »