|
From: Ioannis V. <no...@ya...> - 2001-04-21 19:48:10
|
Guys & girls i need your help. :)
We have the following C++ code:
#include <iostream>
class Matrix
{
double m[4][4];
public:
friend Matrix operator+(const Matrix &, const Matrix &);
};
Matrix operator+(const Matrix &arg1, const Matrix &arg2)
{
Matrix sum;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
sum.m[i][j]=arg1.m[i][j] + arg2.m[i][j];
}
return sum;
}
int main()
{
return 0;
}
It compiles ok. In the definition of operator+ we have the access of private
data members using the . notion directly. The code was taken from
Stroustrup's special edition. In that book i have seen the same access style
of private data members in all member functions of a class. What is the rule
here? Under what circumstances can we access directly private members of a
class?
I hope someone will help to figure it out.
Thanks in advance,
Ioannis
* Ioannis Vranos
* Programming pages: http://members.nbci.com/noicys
* Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ya...> - 2001-09-05 03:23:23
|
If anyone knows, please answer.
=20
The question is about exception handling caught by a constructor. Here
is the code:
=20
class X
{
Vector v;
// ...
=20
public:
X(int);
// ...
};
=20
X::X(int s) try :v(s) // initialize v by s
{
// ...
}
=20
catch(Vector::Size)
{
// exceptions thrown for v are caught here
=20
//...
}
=20
------------------------------------------------------------------------
--------------------------------------------
=20
This is from the bible. The question here is, the constructor's body
ends at the end of catch body? if not, catch() belongs to where.
=20
This style compiles only for constructors defined outside of their
class. For the non-believers code that compiles:
=20
#include <iostream>
=20
class Exception {};
=20
class Vector
{
int size;
public:
Vector(int s):size(s) {}
};
=20
class X
{
Vector v;
public:
X(int);
=20
};
=20
X::X(int s) try :v(s)
{
std::cout<<"code run!\n";
=20
throw Exception();
=20
}
=20
catch(Exception)
{
std::cout<<"Exception caught!\n";
=20
=20
}
=20
int main()
{
X a(7);
}
=20
------------------------------------------------------------------------
--------------------------------------------
=20
=20
=20
Regards,
Ioannis
=20
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
<http://www.noicys.f2s.com/>=20
* Alternative URL: http://run.to/noicys
=20
|
|
From: Jerry v. D. <jv...@at...> - 2002-11-04 22:56:04
|
While trying out g++ 3.2, I did the following:
-----------------------------------------------------------------
#include <vector>
#include <string>
int main(int argc, char* argv[])
{
// std::vector<std::string> arguments(argv[0], argv[argc]);
std::vector<std::string> arguments;
for (int i=0; i<argc; i++)
arguments[i]=argv[i];
}
-----------------------------------------------------------------
which compiles Ok, but segfaults on execution. I did a lot of headscratching
but I cannot figure out why. arguments[i] evaluates to a string type, argv[i]
to effectively a const char*, and string has an const char* constructor.
Help would be apreciated.
--
-- Jerry van Dijk | email: jv...@at...
-- Leiden, Holland | web: users.ncrvnet.nl/gmvdijk
|
|
From: Teodorescu C. <te...@xt...> - 2002-11-05 07:44:31
|
Jerry van Dijk wrote:
>While trying out g++ 3.2, I did the following:
>
>-----------------------------------------------------------------
>#include <vector>
>#include <string>
>
>int main(int argc, char* argv[])
>{
> // std::vector<std::string> arguments(argv[0], argv[argc]);
> std::vector<std::string> arguments;
>
Your *arguments* vector has length 0 you must resize it to argc
or use
arguments.pushpack(argv[i]);
It's simple read :
http://www.oopweb.com/CPP/Documents/STLGuide/VolumeFrames.html
> for (int i=0; i<argc; i++)
> arguments[i]=argv[i];
>}
>-----------------------------------------------------------------
>
>which compiles Ok, but segfaults on execution. I did a lot of headscratching
>but I cannot figure out why. arguments[i] evaluates to a string type, argv[i]
>to effectively a const char*, and string has an const char* constructor.
>
>Help would be apreciated.
>
>
>
|
|
From: Ioannis V. <no...@ho...> - 2002-11-06 00:39:00
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Jerry van Dijk
> Sent: Tuesday, November 05, 2002 12:51 AM
> To: dev...@li...
> Subject: [Dev-C++] C++ question
>
>
>
> While trying out g++ 3.2, I did the following:
>
> -----------------------------------------------------------------
> #include <vector>
> #include <string>
>
> int main(int argc, char* argv[])
> {
> // std::vector<std::string> arguments(argv[0], argv[argc]);
> std::vector<std::string> arguments;
> for (int i=0; i<argc; i++)
> arguments[i]=argv[i];
> }
> -----------------------------------------------------------------
>
> which compiles Ok, but segfaults on execution. I did a lot of
> headscratching
> but I cannot figure out why. arguments[i] evaluates to a
> string type, argv[i]
> to effectively a const char*, and string has an const char*
> constructor.
Because arguments is empty. You will either use push_back() or, you will
create initial objects using the constructor or resize().
>
> Help would be apreciated.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: Daniel G. <sp...@ho...> - 2003-03-21 08:19:55
|
Hi all, I hope you can help.
I've inherited some code, and have a small prob. I have a base class, CBase,
and a derived class, CDerived. What I've then done is to create a third
class, CNew, and there is only one instance of this class (so the members
are all static const, and the object is derived in the header file as static
CNew NewObj. I've put
CNew& NewObj;
as a member in CBase.
Ok? Good now here is the hard bit. I currently access the members of CBase
without the scope reolution operator or by dereferencing, that is I can
write
int Var = MemberOfBase;
in the member function bodies of CDerived. I think this is because of the
constructor: it looks like
CDerived(int x, int y)
: CBase(int x, int y)
{
//...
}
My question is: How do I get the same effect using CNew? How can I do
int Var = MemberOfNew;
? I'm told that it can be achieved as the object of CNew is a reference, but
I'm puzzled as how I dereference a reference so I can write the above
instead of
int Var = NewObj.MemberOfCNew
Is that clear?
Daniel
_________________________________________________________________
Surf together with new Shared Browsing
http://join.msn.com/?page=features/browse&pgmarket=en-gb&XAPID=74&DI=1059
|