Author Topic: Programming Megathread  (Read 145667 times)

Keep in mind that by using new, you're also making your class instance a heap variable instead of a stack variable. This is slow because it takes extra time to allocate the memory on the heap. Always use stack variables when you can.

Yep and it's also safer! Stack unwinding will happen when an exception occurs that has a handler. It's no good putting "delete" at the end of a function body when you're done with a heap-allocated object if the function can throw an exception.

Smart pointers can solve a lot of issues like this one.

When creating a class using new, it will not be deleted when it goes out of scope. You must delete it some point when you're finished with it, or you will leak memory.
Ah, yes, I forgot this aspect of C++.
I'm used to C#, where new is the only way to create a class instance

Ah, yes, I forgot this aspect of C++.
I'm used to C#, where new is the only way to create a class instance

I should also add that in c++ new will return a pointer, so the correct code would actually be:

Code: [Select]
thingHandler* TH = new thingHandler(20);
Additionally, If you want a more compact statement, you can use the auto keyword.

Code: [Select]
auto TH = new thingHandler(20);


The example code has a few more issues that need to be addressed. Firstly, access modifiers are not set by prepending them to each member, instead you section them off like so:

Code: [Select]
class thingHandler
{
   bool thingsAreHandled = false; // class members are private by default
   
public:
   thingHandler(int start);
   void handleThings(); // member function
   int things;
};

Secondly, there was no declaration for the constructor, which I've added to the code above. Finally, knowing new returns a pointer and memory allocated with it must be explicitly released, the main function should be adjusted as follows:

Code: [Select]
int main()
{
    auto TH = new thingHandler(20);
    TH->handleThings(); // use -> to access members of a pointer
    TH->things += 10;
    delete TH;
}


Woah
What does this mean?

okayokayokay
so you define a class, you can set private or public members (vars or funcs) inside the class and then you define the object name after the closing braces
then you can define functions outside the class by using (classname)::(funcname)?
and you need the :: to give the scope of the function of the class


why do people so often like to say something like char *s instead of char* s
the way I see it is that the type is "a pointer to a char". therefore I feel like the asterisk should be part of the type. I really can't see any reason, logically, to prefer putting it on the variable name, other than maybe the fact that you'd be doing that to access the value later

in googling something to make sure I was correct, I saw that int *x, y results in an int pointer and then.... a plain int? so I guess that could be a reason to place the asterisks next to the variable name. int* x, y is a little unclear
but then I also think that placing multiple declarations on the same line is ugly

why do people so often like to say something like char *s instead of char* s
the way I see it is that the type is "a pointer to a char". therefore I feel like the asterisk should be part of the type. I really can't see any reason, logically, to prefer putting it on the variable name, other than maybe the fact that you'd be doing that to access the value later

in googling something to make sure I was correct, I saw that int *x, y results in an int pointer and then.... a plain int? so I guess that could be a reason to place the asterisks next to the variable name. int* x, y is a little unclear
but then I also think that placing multiple declarations on the same line is ugly

honestly the way I see it is that pointers aren't necessarily types, so it might be confusing for people who are learning types to put it as char* s

moreover, the way I read it is "a char pointer towards variable s" so it just works better for me with char *s

honestly the way I see it is that pointers aren't necessarily types, so it might be confusing for people who are learning types to put it as char* s
a char is obviously distinct from a pointer to a char, and for me that's enough to qualify it (conceptually) as a separate type
and I read the statement as "char pointer named [name]". char pointer, then the name. I guess I could see it the other way too, but it doesn't look as clear to me
« Last Edit: March 10, 2017, 12:01:24 AM by Foxscotch »

a char is obviously distinct from a pointer to a char, and for me that's enough to qualify (conceptually) it as a separate type
and I read the statement as "char pointer named [name]". char pointer, then the name. I guess I could see it the other way too, but it doesn't look as clear to me

this is where I stand

a char is obviously distinct from a pointer to a char, and for me that's enough to qualify it (conceptually) as a separate type
and I read the statement as "char pointer named [name]". char pointer, then the name. I guess I could see it the other way too, but it doesn't look as clear to me
idk the way i justify it is because it looks nice to me and a read it like

"this char type variable points to S" whereas you see it as "this character pointer is equal to s"

idk why I do it tbh

[img ]https://i.imgur.com/u6o04HM.jpg[/img]

Being that I am finally learning C++, I have immense appreciation for this image

Code: [Select]
def coolsisLogin(u, p):
    blah =  {'alias':'spa', 'memberName':'LoginStudent', 'dataSourceName':'', 'operationType':'call'}
    blah['_transaction'] = '<transaction xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:type="xsd:Object"><operations xsi:type="xsd:List"><elem xsi:type="xsd:Object"><clientTypeCode>FTP</clientTypeCode><username>%s</username><password>%s</password><operationConfig/></elem></operations></transaction>' % (u, p)
    r = requests.post('https://(myschool).sis.cool/Gateway.ashx?outputXML=true', data = blah)
    pp = r.text.decode('utf-8')
    xmlParser = ET.fromstring(pp.replace("utf-16", "utf-8"))
    response = xmlParser[1].text
    return response
Why does this need to be so damn complicated just to get a session