thanks mike... that was very useful :)
i'm sorry bout my very limited knowledge in c++
subith86 17 Junior Poster
subith86 17 Junior Poster
thanks mike... that was very useful :)
i'm sorry bout my very limited knowledge in c++
thanks, but I already knew this.
Just wanted to know if there is any shortcut like this as we have in Java
int[] a = new int[]{4,5,6,7,7};
Hi friends,
One of my class member function is supposed to return an array of integers. Here's the function.
int* MyClass::getallVars()
{
int* a = new int;
int b[] =
{
GetparentVar1(),
GetparentVar2(),
GetchildVar1(),
GetchildVar2(),
};
a=b;
return a;
}
Is there anyway I can do this without the use of second array "b"?
Did you ever set any value to your variable 'commonVar1' before calling GetcommonVar1() method?
there is no setter method. I intentionally did it like this to see a garbage value.
I don't see any reason why you should declare a variable in the child class having the same name as the parent's.
yes I know. This is not a professionally written code. I was learning inheritance and was trying out different combinations.
If it's a coincidence then i'll have to check with other compiler. But i feel this is not a coincidence.
Did you ever set any value to your variable 'commonVar1' before calling GetcommonVar1() method?
there is no setter method. I intentionally did it like this to see a garbage value.
I don't see any reason why you should declare a variable in the child class having the same name as the parent's.
yes I know. This is not a professionally written code. I was learning inheritance and was trying out different combinations.
If it's a coincidence then i'll have to check with other compiler. But i feel this is not a coincidence.
Hi friends,
I was trying out some inheritance related stuff and found something. I just want to know the reason why.
I have a ParentClass and a ChildClass. Both have an integer named commonVar1. Also both have a getter method of the same name. Here goes the code.
class ParentClassOT
{
public:
ParentClassOT();
~ParentClassOT();
int commonVar1;
int GetcommonVar1() { return commonVar1; }
};
class ChildClassOT : public ParentClassOT
{
public:
ChildClassOT();
~ChildClassOT();
int commonVar1;
int GetcommonVar1() { return commonVar1; }
};
In my main method I'm doing something like this
ChildClassOT obj1;
cout<<obj1.GetcommonVar1();
Now I'll give out the results of different combinations I have tried
1. Code as it is : outputs garbage
2. Parent getter (line 7) commented : outputs garbage
3. Child member variable (line 6) commented : outputs garbage
4. Child getter (line 7) commented : outputs -1
5. Child getter and member variable commented : outputs garbage
I'm okay with results 1,2,3 and 5. But I don't get why 4th one gives a -1.
Could somebody please help... :confused:
thanks for all your help. i'll check out this problem with my colleagues on monday... till then i'll mark this as solved
g++ -Wall main.cpp myclass.cpp friendclass.cpp -o friend
I have never done the compilation from the command line. Does this order make a difference?
If then that should be my problem cos I'm doing from an IDE.
I am able to compile and link using g++, with changes below.
1. in FriendClass.h remove MyClass.h include and provide forward declaration for MyClass
2. In MyClass.h include FriendClass.h
I too tried this, but same error. It'd be better if you could post your code.
Let me pull your files into a project and see what happens.
Thanks, That would be great...
@Jonsca : I'm compiling it using IDE - Code::Blocks. But what difference it makes
@Sky Diploma : Your suggestion didn't work. Giving the same error as I posted previously, but this time the error is on MyClass instead of FriendClass.
You need to "forward declare" FriendClass at the top of MyClass.h. On line 6, put
class FriendClass;
.
Didn't work.
It's giving these errors.
error: invalid use of incomplete type 'struct FriendClass' (line 13 : MyClass.h)
error: forward declaration of 'struct FriendClass' (line 6 : MyClass.h)
Hi friends,
I was understanding the concept of friend functions. I read in my book that there are two possible ways of "making friendship":cool:
1. Make a class a friend.
2. Make the method in a class a friend.
I am able to write a code for the first point. But when I tried the second one, I got the error as said in the title.
Here is my piece of code. To make this less lengthier I have put .h and .cpp file in one code block.
/****************** MyClass.h ***************/
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass
{
public:
MyClass();
int GetprivVar1() { return privVar1; }
//friend class FriendClass;
friend void FriendClass::changeMyVariable(MyClass*);
protected:
private:
int privVar1;
};
#endif // MYCLASS_H
/****************** MyClass.cpp ***************/
#include "../include/MyClass.h"
MyClass::MyClass()
{
privVar1 = 0;
}
/****************** FriendClass.h ***************/
#ifndef FRIENDCLASS_H
#define FRIENDCLASS_H
#include "MyClass.h"
class FriendClass
{
public:
FriendClass();
void changeMyVariable (MyClass *);
protected:
private:
};
#endif // FRIENDCLASS_H
/****************** FriendClass.cpp ***************/
#include "../include/FriendClass.h"
FriendClass::FriendClass()
{
//ctor
}
void FriendClass::changeMyVariable(MyClass* obj)
{
obj->privVar1 = 9;
}
#include <iostream>
#include "include/FriendClass.h"
#include "include/MyClass.h"
using namespace std;
int main()
{
MyClass obj1;
FriendClass obj2;
cout<<"before : "<<obj1.GetprivVar1();
obj2.changeMyVariable(&obj1);
cout<<"after : "<<obj1.GetprivVar1();
return 0;
}
Here I'm talking about lines 12 and 13 in first code block (MyClass.h).
If I make a class a friend it works, but making friendship with the function is giving compiler error
error: 'FriendClass' has not been declared
Hi friends,
Thanks for all your support. I found it with the help of another colleague.
I can see memory increasing even if cout statement is there. But it was very slow increase, atleast some 20 times slower. I think this is exactly what is mentioned in Zjarek's post.
--
Regards,
Subith
@L7Sqr :
I tried your suggestion
int main()
{
char* a;
try
{
for (;;)
{
//cout<<(void*)a<<endl;
a = new char[1024];
}
}
catch (std::bad_alloc ba)
{
std::cerr << "Caught bad alloc. Waiting for input..." << std::endl;
std::cin.get ();
}
}
The exception is getting caught. But if I uncomment the line 9, it doesn't throw any exception.
@narue: I changed the code like this. Hope this is what you meant :(
int main()
{
char* a;
/* for (;;) {
a = getName();
//cout<<a;
}
*/
for (;;)
{
cout<<(void*)a<<endl;
a = new char[10];
}
}
Well my concern is not about the address. I just wanted to know why memory usage is not getting increased. I think this cout does a delete internally, or something like that???
Yes, I tried it. Now i get incremented addresses in each loop. but i dont see memory increasing.
I'm sorry, I didn't get what void* does.
anyway memory is still not increasing when the cout is there...
Hi friends,
I know this is such a freak code, but out of curiosity I did it to see memory usage increase using new operator.
int main()
{
char* a;
for (;;)
{
//cout<<&a<<endl;
a = new char[10];
}
}
As you see line7 is commented. When i run this program, obviously a linear increase in memory usage. I could verify it from windows task manager.
But if I uncomment the line7, I could see in the console that same address is getting printed, which means that I don't see any increase in memory. Why is that?
thanks jonsca for the reply
I know what you are trying to say. I know if a local variable has the same name as the global variable then the global one will be ignored. I also know local variable will be destroyed while the program exits out of the function.
But my concern is how uncommenting lines 12 and 14 in main function affects the farm1 function.
1. with the comments, line6 prints garbage value
2. without the comments, line6 prints 10.
while i was learning about global variables I did some trials and found one problem. Here goes my code.
int cows = 10; //global variable
void farm1()
{
int cows;
cout<<"in farm1, cows="<<cows<<endl;
cows++;
}
int main()
{
//cout<<"in main, cows="<<cows<<endl;
farm1();
//cout<<"back in main, cows="<<cows<<endl;
}
Here line 6 prints some garbage value as expected. But when I uncomment lines 12 and 14, line 6 prints 10. Can somebody please explain why?
thanks all... got it :)
Yes, even I'm surprised with line 12. That's why I posted it here in case anybody has an answer for that.
I get the point in line 3 though
char* x represents a string.
if cout x, then it'll print the whole string
if cout *x, then it'll print the first character in the string. Maybe since my string is only 1 character long, you got confused.
Can anybody tell me how to get the memory address where the character 'a' is located?
I'm trying something like this
void receiver (char* x)
{
cout<<x<<endl; //prints "a"
cout<<*x<<endl; //prints "a"
cout<<&x<<endl; //prints address of x
}
int main()
{
char p = 'a';
cout<<p<<endl; //prints "a"
cout<<&p<<endl; //prints "a"
receiver(&p);
}
In the main function for both p and &p, it is printing the value of p.
If it is int instead of char, then p would print the value and &p would print the address. Why is this strange behavior with char?
ok thanks :)
so it seems like I have to invoke methods, then I should stick with if-else
In C++ I'm able to do something like this
(some_condition)? function1():function2();
When i tried the same thing in Java it's not possible. It wants some variable to hold the result even though the return type of function1 and function2 are void.
Is there any other way to do it other than if... else...?
Here is my piece of code:
int b[2][2] =
{
{4,6},
{-3,0}
};
int b_length = (sizeof b)/sizeof(int);
int *sub_b = b[1];
b_length will have the length of 2-D array b (4, in this case).
now sub_b is the sub-array of b at row 1, which is {-3,0}
Is there any way to find the length of sub_b other than the below technique.
int sub_length = (sizeof b[1])/sizeof(int);