Jump to content

1.How to use ->?

2.What is trailing return type and how to use it?

3.What does const_cast do and when to use it?

4.Is overloading functions helpful?

5.How to use a pointer to an array after it is returned by a function(and why would we want a function to return a pointer to an array)?

6.Parameters and arguments are confusing to me any suggestions?

Link to comment
https://linustechtips.com/topic/38244-functions/
Share on other sites

Link to post
Share on other sites

what language are you talking about

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
https://linustechtips.com/topic/38244-functions/#findComment-491870
Share on other sites

Link to post
Share on other sites

what language are you talking about

Looks like C/C++

 

1.How to use ->?

2.What is trailing return type and how to use it?

3.What does const_cast do and when to use it?

4.Is overloading functions helpful?

5.How to use a pointer to an array after it is returned by a function(and why would we want a function to return a pointer to an array)?

6.Parameters and arguments are confusing to me any suggestions?

1) I'd like to know as well. It's something to do with pointers I think

2) http://publib.boulder.ibm.com/infocenter/zos/v1r13/index.jsp?topic=%2Fcom.ibm.zos.r13.cbclx01%2Ftrailing_return.htm It appears to be where you can have the same function return multiple different types of variable, depending on what is passed to it. eg. if you had a multiplication function and you didn't know what it would be used for, it could be a double and an int function, so if you get whole numbers you return an int, but if you get decimals, you return a double. I don't really know how to use it, but that article and other google results do. Looks complicated though.

3) Const_cast allows you to convert from a constant variable to a volatile variable and vice-versa. http://www.cprogramming.com/reference/typecasting/constcast.html

4) Overloading can be helpful if you have multiple functions which do the same thing in slightly different ways (eg. a square root function that you can pass an int, float double etc.), but just for ease of reading the code, I think it's best in general to stay away from them under most circumstances.

5) As far as I know, you can't return the whole array, you can just return a pointer, but I'm not certain. I have run into some problems with it, so I would like to see what somebody more experienced says about it.

6) What do you mean? The difference between them? A parameter can be any value of a specified variable type, but an argument is a predetermined value which the code interprets as how it should run. eg. on the windows shutdown command, you call

shutdown.exe /s /t 10 /c "Comment!!!"

to shutdown the computer with an idle timeout of 10 seconds. the /s /t and /c are arguments, and the 010 and message are parameters. There are predetermined arguments, and you can only chose the ones that the application has coded for, but the parameters can be any value you like (usually a specific type of variable within a specified range).

HTTP/2 203

Link to comment
https://linustechtips.com/topic/38244-functions/#findComment-492138
Share on other sites

Link to post
Share on other sites

 

1) I'd like to know as well. It's something to do with pointers I think

 

 

It's used when accessing class members from a pointer to a class

 

Normally if you declare a class using

MyClass myClassObject;

you can access it's members using a "."

myClassObject.someMember;

However if you have a pointer to a class

MyClass *myClassObject;

Then you need to use "->" to access it's members

myClassObject->someMember;

This also applies to structures

Current Rig (Ongoing Build)


Spec:- 4770K | GTX 780 | 32Gb 2133Mhz Vengeance Pro | CaseLabs TH10 | 2 x 840 Pro RAID 0 | 3 x 3Tb WD Red RAID 5 | Maximus VI Formula | LSI MegaRAID 9271


Cooling (Ongoing Build) :- EK CSQ Clean | EK FC Titan | 3 x BlackIce SR-1 480mm | NB eLoop Fans | Aquaero 5 XT | Dual D5 | Aqualis XT Res

Link to comment
https://linustechtips.com/topic/38244-functions/#findComment-492267
Share on other sites

Link to post
Share on other sites

5) As far as I know, you can't return the whole array, you can just return a pointer, but I'm not certain. I have run into some problems with it, so I would like to see what somebody more experienced says about it.

 

For all purposes, arrays are just pointers.  They are pointers to the first item in the array.

 

A pointer is a memory address.  If the value of a pointer is 1234, then the data for your array begins at memory address 1234.  If your array was an array of 32-bit signed integers, then the value of the first item in the array is at address 1234 to 1237 (4 bytes), and the second item is at 1238 to 1241, and so on.

 

You can take any pointer and treat it like an array.  One popular array of de-referencing a pointer is to use the asterisk operator.  For example, if your pointer's name is "numbers", then you can de-reference the pointer to get the value at address 1234 by calling:

 

int value = *numbers;

 

You can also do this and get the same result:

 

int value = numbers[0];

 

Effectively, you are adding zero offset to the pointer, and then de-referencing it.  To get the second number, you would use:

 

int value = numbers[1];

 

The square-brackets operator is effectively saying: add 4 bytes offset to the numbers pointer and then de-reference it to get the value (memory address 1238 to 1241).  Why 4 bytes?  Because the size of "int" is 4 bytes.  This also means you can equivalently do:

 

int value = *(numbers + 1);

 

You are adding 1 sizeof(int), which is 1 4-byte offset.  This is why sometimes you see funny-business in a for-loop:

 

for(int* p = numbers; p != &numbers[10]; p++) {

  int value = *p;

}

 

The p++ increment operator is adding 1 sizeof(int) to the pointer.  You are just moving the pointer along the array, 1 by 1.

 

Everything I've said here is true for other types, including "short" (2 bytes in size), char (1 byte in size, typically), double (8 bytes in size).  It is also true for structs:

 

typedef struct _Point {

  int x;

  int y;

} Point;

 

The size of this struct, which is sizeof(Point) is 8.  You can iterate through an array of Points easily, and the pointer offset by each 1 offset is by 8 bytes each time.

 

Because of all of this, I favor defining pointers with the asterisk on the type, not on the variable name.  For example:

 

int* numbers;  // good

int *numbers;  // bad

 

I feel this way because you are defining a type, and the type is "int pointer".

Link to comment
https://linustechtips.com/topic/38244-functions/#findComment-496039
Share on other sites

Link to post
Share on other sites

the arrow is as JamesThresher stated used to access a member of a pointer.

 

Note these lines are equivalent....

 

(*object).member

object->member

 

The problem here is that the dot operator has precedence over the member access operator and thus:

 

*object.member would be doing something completely different.

 

On a different note, overloading functions is extremely helpful especially when using Abstract Data Types.

Link to comment
https://linustechtips.com/topic/38244-functions/#findComment-496773
Share on other sites

Link to post
Share on other sites

I have another question, but it doesn't have to do with this topic.How to use system() or shell execute to open exe?And if i wanted to write a program to ask the user what he or she wanted to open what would that program look like?

System=bad. And it gets picked up by antiviruses. To use ShellExecute, you need to #include then the prototype (from the MSDN website) is:

HINSTANCE ShellExecute( //You can probably ignore the return value for what you'd use it for - it's a handle to the process which you create. However, if you check that it's <=32, you can check for an error (>32 is good) HWND hwnd, //optional handle to the window you're calling from - "Parent Window" - If you're using Win32 C++ you get a HWND, if not, you don't LPCTSTR lpOperation, //An optional string containing the command you want to to do. Options are below. LPCTSTR lpFile, //A string containing the file name of the file you are opening LPCTSTR lpParameters, //An optional string with all command line parameters you would like to pass to the program LPCTSTR lpDirectory, //An optional string with the directory with the file you are opening. It can be relative to the working directory of the program, and if set to NULL it will open from the working directory int nCmdShow //A flag/combination of flags telling the window how to open. Options are below.)

lpOperation can be set to:

  • "edit" which will open in the default editor;
  • "explore" which will open the folder set by lpFile (not lpDirectory)
  • "find" which will start a search in lpDirectory. It might search for the contents of lpFile, but I don't know
  • "open" which will open the file in it's default program or a folder in windows explorer
  • "print" which will print the document if the file supports it
  • NULL which will use the default, then if not it will open, and if that fails it will go through the other functions.

nCmdShow can take the following flags:

  • SW_HIDE which starts it hidden (not minimized but hidden)
  • SW_MAXIMIZE, SW_MINIMIZE and SW_RESTORE are self explanatory
  • SW_SHOW shows and activates the window, and SW_SHOWDEFAULT sets it to the default size, shows and activates it.
  • SW_SHOWMAXIMIZED and SW_SHOWMINIMIZED shows and activates the window, and sets it to the max/min state
  • SW_SHOWMINNOACTIVE shows and minimizes the window without activating it
  • SW_SHOWNA shows the window in it's current size/state and doesn't activate it/bring it to the front
  • SW_SHOWNOACTIVATE shows the window in it's most recent size and position but doesn't activate it
  • SW_SHOWNORMAL activates and displays the window, and sets it to the automatic size and position.

For example, to open "Shutdown.exe" with the parameters '/f /c "Goodnight"', you would do this:

#include <Windows.h> ...if(ShellExecute( NULL, "open", "shutdown.exe", "/f /s \"Goodnight\"", "C:\\Windows\\System32\\", SW_HIDE)<=(HINSTANCE)32){ /*You've got a problem*/ }

HTTP/2 203

Link to comment
https://linustechtips.com/topic/38244-functions/#findComment-496889
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×