« first day (226 days earlier)      last day (2633 days later) » 

user2015064
5:07 AM
Hey guys, I wonder what is the better code style?

void func(int a, double b)
{
return (a + b);
}

Or :

void func( int a, double b )
{
return ( a + b );
}

?
 
1 message moved from Lounge<C++>
 
user2015064
Which one is better?
 
5:48 AM
@xersi both suck because you use parens in a return statement
Also I hardly ever see anyone inserting spaces after the beginning paren and a space before ending paren
 
 
4 hours later…
user2015064
9:45 AM
Wow I did not know that is bad style, I will try not to insert unnecessary parentheseses around non-complex return statement.
 
return a + b; is just as understandable
in fact, the usual reaction when people see return(a + b); is "hey, does the guy who wrote this even know the language?"
 
Is this an appropriate place to discuss a C++ Windows DLL export issue?
 
you may ask, sure. you aren't guaranteed to get the answer though
 
I created a windows DLL with Cmake, and the import library (Live555.lib) shows the following name mangling:

2470.Live555.dll ?isSource@Medium@@UBE_NXZ

However, my executable tries to link this, and fails:

LNK2001 Unresolved external ... (?isSource@Medium@@UBEEXZ)

Note the different mangling
Can the different name mangling be the cause? Why does it happen?
 
the parameter types are different, or the compiler used for a lib and the program were different, and so on
 
10:01 AM
What do you mean by parameter types?
 
void f(int) vs void f(long)
 
Thankee sai
 
10:15 AM
you can force a specific name using the EXPORTS section in the def file
 
Thanks, I found the solution by using the undname tool from Visual Studio Command line, I had a different parameter type as suggested
It's only applyable to this library I'm building as the type Boolean had a conditional definition.
 
 
8 hours later…
5:55 PM
I come from the perfect land of the JavaScripts and venture into the basics of this...
ClassName::ClassName( int a, int b ){

  ClassName self = *this;
  self.a = a;
  self.b = b;
}

vs

ClassName::ClassName( int a, int b ){

  this -> a = a;
  this -> b = b;
}
 
2 messages moved from Lounge<C++>
 
as a JS person, the first way seems very sensible. Does it actually matter or should I grow accustomed to the second version?
more of a style question :P
 
nwp
@towc The first version doesn't work at all, because you make a copy of *this and set self.a and self.b but never set this->a and this->b.
you probably meant to do ClassName &self = *this; to make a reference instead of a copy
 
so what does one do if they want to use dot notation?
oh
maybe
 
nwp
the proper C++ way to do this is ClassName::ClassName(int a, int b) : a(a), b(b){}
 
6:01 PM
oh boy...
I'll understand that eventually
thanks
 
nwp
As a general advice, don't try to write JavaScript in C++. It turns out that C++ is much worse at being JS than JS.
 
@towc you don't
 
@nwp nice advice :P
and alright
 
 
1 hour later…
7:13 PM
so, I made a very very ugly prime finder:
#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main(){

  string amountStr;

  cout << "up to what num? ";
  getline( cin, amountStr );


  int amount = stoi(amountStr);
  int primesSoFar = 1;
  vector<long> primes;
  primes.insert( primes.begin(), 2 );

  cout << "beginning" << endl;
  cout << "1: 2" << endl;

  string primesStr = "2 ";
  for( int i = 3; i < amount; ++i ){

    int root = ceil(sqrt(i));
    bool isPrime = true;

    for( int j = 0; j < primesSoFar; ++j ){
there's obviously a lot that could be better. This doesn't feel like something that is supposed to be this way
any pointers as to what's very wrong? (it compiles ok and works ok, that's not the issue)
 
nwp
instead of reading in a string and converting it to an int you may as well read an int directly
 
that's a thing?
 
nwp
yes
 
guessing not with getline
I'll find out
oh, right...
cin >> num
 
nwp
and you should consider putting that inside an if to check if reading succeeded
 
7:17 PM
#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main(){

  int amount;
  int primesSoFar = 1;
  vector<long> primes;
  primes.insert( primes.begin(), 2 );

  cout << "up to what num? ";
  cin >> amount;

  cout << "beginning" << endl;
  cout << "1: 2" << endl;

  for( int i = 3; i < amount; ++i ){

    int root = ceil(sqrt(i));
    bool isPrime = true;

    for( int j = 0; j < primesSoFar; ++j ){

      int prime = primes[ j ];

      if( prime > root ){
ok
 
nwp
primes.insert should probably be primes.push_back
 
#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main(){

  int amount;
  int primesSoFar = 1;
  vector<long> primes;
  primes.insert( primes.begin(), 2 );


  cout << "up to what num? ";
  cin >> amount;

  if( !amount ){
    cout << "you don't like games, do you?";
    return 0;
  }

  cout << "beginning" << endl;
  cout << "1: 2" << endl;

  for( int i = 3; i < amount; ++i ){

    int root = ceil(sqrt(i));
    bool isPrime = true;

    for( int j = 0; j < primesSoFar; ++j ){
 
nwp
or maybe it should be vector<long> primes{2};
 
@nwp would have thought that it's arguable. Ideally it would have been primes[0] = 2, but got some boundary errors...
I thought that it should have worked
@nwp oh, that's probably it
 
nwp
@towc there you access the first element of primes, but it doesn't have a first element because it is empty, so it fails at runtime
 
7:22 PM
I think I see
 
nwp
Some of that if/else/isPrime/j=primesSoFar stuff can probably be simplified with break/continue
 
do people actually use those?
they're there in JS as well, but they confuse more often than solve
thanks for everything, I'll be gone for a while :)
 
nwp
7:37 PM
@towc From my perspective using isPrime and j = primesSoFar to simulate break and continue are confusing. As a C++ programmer you are expected to know what keywords do.
 
7:54 PM
don't do primes.insert( primes.begin(), 2 );
also if(!amount) is the same as if(amount == 0), except more obfuscated
 

« first day (226 days earlier)      last day (2633 days later) »