Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 530 531 [532] 533 534 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882959 times)

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #7965 on: September 25, 2015, 06:11:17 pm »

I've had a look at the C# reference, and it seems that the override is in fact unnecessary.

Having said that, the compiler complains that there is already a method called ToString if I omit the override.

"'Inventory.ToString()' hides inherited member 'object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7966 on: September 25, 2015, 07:25:35 pm »

I've had a look at the C# reference, and it seems that the override is in fact unnecessary.

Having said that, the compiler complains that there is already a method called ToString if I omit the override.

"'Inventory.ToString()' hides inherited member 'object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."

Shouldn't it be something like "public string InventoryClass::ToString()"?
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7967 on: September 25, 2015, 07:45:13 pm »


Sounds trickier than it needs to be, but I can recommend the subprocess library for such tasks.
Yes, I was going to use subprocess, but I would have needed the command for python!

Thanks for sys.executable, that is indeed what I need.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7968 on: September 25, 2015, 08:12:49 pm »

Also, true and non-zero are not the same thing.
That's one of those wiggly language-based things. In at least a handful of languages (like C++), evaluating a many things (notably numbers) as a boolean is equivalent to asking "is it not zero?", so these two would be equivalent:
Code: [Select]
if (num != 0) {
    do stuff
}

if (num) {
    do stuff
}
Meaning-wise they aren't the same, obviously, but implementation wise they are identical in a fair number of scenarios in a fair number of languages.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7969 on: September 25, 2015, 09:02:04 pm »

I feel that a lot of common languages tend to implicitly convert  0 to false and 1 to true. Python does, and so do Java (i think ), C, C++, maybe C#, maybe Lua. Drfinitely PHP and Javascript.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7970 on: September 25, 2015, 09:06:17 pm »

For pointers, you can just check

if(ptr)
   ptr->doThing();

and it converts not null/null to true/false. Good to know because you come across this all over the place in programs. In C# you see this to avoid errors from null references. C# reference is basically a C++ pointer.
« Last Edit: September 25, 2015, 11:00:32 pm by Reelya »
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7971 on: September 25, 2015, 10:44:48 pm »

I feel that a lot of common languages tend to implicitly convert  0 to false and 1 to true. Python does, and so do Java (i think ), C, C++, maybe C#, maybe Lua. Drfinitely PHP and Javascript.
Booleans are actually just integers in C languages. C doesn't even have the bool type. 0 is False, everything else is True. Null pointers have a value of 0, and a lot of functions return 0 for various reasons.

E.g.:
if (cin)
    //no error flags, okay to read input
« Last Edit: September 25, 2015, 10:51:20 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7972 on: September 25, 2015, 10:55:24 pm »

I'm agreeing with TheBiggerFish, if he's getting at what I think he is.
You tell me, I think I completely missed the bit where it wasn't Java.
this.apply(palm, face, Math.INFINITY);
(I think.)
But yeah, I can confirm that Java has 1 be true if passed to a conditional.

In unrelated news, can I suggest that we put the language we're coding in at the top of the code section?  Just for ease of identification.
Because it took me until Right NowTM to notice that the code I was talking about was in a language I knew nothing about.
« Last Edit: September 25, 2015, 10:57:56 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7973 on: September 25, 2015, 10:58:45 pm »

C# doesn't implicitly convert null to false, though.

And C++ gives a warning when you convert int to bool.
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7974 on: September 26, 2015, 02:55:04 am »

Hey guys where can I find some good examples that show the usage of pointers and references in C++? The cases when they're necessary, how to pass them to function, and so on?
For example, does anybody know if something is wrong with this?

Student &test(Student s) // Student is the name of a class
{
   return s;
}

and this?

Student test(Student &s)
{
   return s;
}

and this?

Student &test(Student &s)
{
   return s;
}

and this?

Student test(Student s)
{
   return s;
}

What's the difference between either of those?
« Last Edit: September 26, 2015, 03:08:02 am by RoguelikeRazuka »
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7975 on: September 26, 2015, 03:03:46 am »

ponters and references are, I think, the same thing.

You use them whenever you don't want to make a copy of a class, either for performance reasons, or because you want to modify the original.
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7976 on: September 26, 2015, 03:08:53 am »

The only really important difference that I know of between the two is that valid pointers can be null and valid references can't (oh, and you have to dereference pointers with '*' but you can't dereference references [which is a good example of stupid naming :P]).

Beyond that they are basically the same, and in most instances compilers actually implement references internally in the end-code as a variation of a pointer.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7977 on: September 26, 2015, 03:50:02 am »

What's the difference between either of those?

« Last Edit: September 26, 2015, 04:17:50 am by Reelya »
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7978 on: September 26, 2015, 04:15:19 am »

Now I see. But what the 'const' modifier basically means? How the complier treats an object modified with const?
Also, what are ways of passing a two-dimensional array to function?
« Last Edit: September 26, 2015, 04:19:01 am by RoguelikeRazuka »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7979 on: September 26, 2015, 04:18:57 am »

if something is const then it's a compiler error to try and change it. So a const variable parameter can only be set when a function is called, and not changed inside the function. in this case, they're still stored as normal variables, but it's just illegal to write code that tries and change them. So, const can be good to ensure a value never gets changed by accident if it shouldn't.

However, it has another use. If you write literal values into the program:

const int max_size = 256;

^ unlike a normal int, this gets compiled down to a literal value of 256 in machine code wherever it's used. Otherwise, variables are pointers which need to get dereferenced to a real value whenever they're used. So putting const for literal values like this reduces executable size and speeds up the program.

In more recent C++ versions, there's also const methods for a class:

void MyClass::MyMethod() const;

^ adding const like this means the whole method is const. For these, it's disallowed to change any values in the object within this method. It's basically a guarantee for that method that "this method is a data-retriever only, and does not change the object's state". Also, if you make two copies of the same method, one with const and one without, then for a const object, it will use the const version of the method. Which could be handy I guess, but I've never used it.
« Last Edit: September 26, 2015, 04:29:02 am by Reelya »
Logged
Pages: 1 ... 530 531 [532] 533 534 ... 796