|
When you want something other than the default behavior.
|
|
|
|
|
provide some example
ranjith
|
|
|
|
|
If this was a write code to order service, what sort of an example would I write for you? But since this is not a coding service, no, I won't do your homework for you.
|
|
|
|
|
|
Copy constructors - cppreference.com[^]
Default Copy Constructor - C++ Forum[^]
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Basically when you need custom logic in copying. For example, dynamic memory allocation in pointer members.
Same thing is applicable in assignment operator overloading too.
|
|
|
|
|
There are tons of examples out there, here is but one[^].
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
May be it is orthogonal information, If we want to hide/restrict usage of default copy constructor, we need to provide override declaration in private section of class, and no definition for that. This ensures preventing usages of copy constructor
|
|
|
|
|
I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages):
[^]
There also other methods that involves making swapping element by element but it is hidden:
std::swap(array1,array2);
It would be better using pointers, with only 3 operations all the elements are swapped:
double *a,*b; two arrays
double **ptr1,**ptr2,**ptrx;
ptr1=&a;ptr2=&b;
Swapping:
ptrx=ptr1;ptr1=ptr2;ptr2=ptrx;
Example code:
<pre lang="c++">
#include <iostream>
using namespace std;
double *a;
double *b;
class c_class { public: double x; } ;
void main()
{
a = new double[7];
b = new double[7];
long i;
for (i = 0; i < 7; i++)
{
a[i] = i + 100;
b[i] = i + 300;
}
double **ptr1 = &a,**ptr2=&b,**ptrx;
cout << "=== SWAPPING ARRAYS ===" << endl;
cout << "STEP 1: ptr1= " << endl;
for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
cout << " ptr2= ";
for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
cout << endl;
cout << " Swapping the arrays" << endl;
ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
cout << "STEP 2: ptr1= ";
for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
cout << " ptr2= ";
for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
cout << endl;
cout << " Swapping the arrays" << endl;
ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
cout << "STEP 3: ptr1= ";
for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
cout << " ptr2= ";
for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
cout << endl;
cout << "\n\n=== SWAPPING CLASSES ==="<<endl;
c_class clas1, clas2;
clas1.x = 1000.1;
clas2.x = 5000.5;
c_class *pc1, *pc2,*pcx;
pc1 = &clas1; pc2 = &clas2;
cout << "Class 1 and 2 x value: " << pc1->x << " "<<pc2->x<<endl;
cout << " Swapping the classes " << endl;
pcx = pc1; pc1 = pc2; pc2 = pcx;
cout << "Class 1 and 2 x value: " << pc1->x << " " << pc2->x << endl;
cout << "\n\n======end========\n"; getchar(); getchar();
The only problem is that the arrays must be declared initially as pointers. If they are declared as:
double a[7];, the vs2013 does mark pointer assigment as an error:
double *ptr=a;
Is there a way to solve it?
|
|
|
|
|
All you are doing there is changing the pointers, which makes no real sense. What problem are you trying to solve?
|
|
|
|
|
Given his code it will be a sort routine like bubble sort, but it looks like homework to me so I am reluctant.
Usually they are asked to do an in place array sort and it. He hasn't worked out how to typecast the pointers properly and it seems to escape him that you can just malloc the arrays which is what his "new" statement hides.
In vino veritas
modified 20-Sep-16 6:53am.
|
|
|
|
|
The idea is use the pointers to avoid copying all the data.
An example, if there are 3 arrays of pixels: A, B, C and I want to order the arrays I can make the following operation:
if (distance(A,B)>distance(A,C))
swap(B,C);
I tried this code that swaps anything but it does not compile:
#include <iostream>
#include <string>
using namespace std;
void swap(void *&a,void *&b)
{
void *x=a;a=b;b=x;
}
void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
cout << "a:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
cout << "b:"<<b[0]<<" "<<b[1]<<" "<<b[2]<<endl;
swap((void *) a,(void *) b);
cout << "a:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
cout << "b:"<<b[0]<<" "<<b[1]<<" "<<b[2]<<endl;
cout << "=== END ===" << endl; getchar();
}
modified 18-Aug-17 5:26am.
|
|
|
|
|
Why are you using void* in your swap method and calls? Void pointers do not point to anything so the compiler cannot figure out what to do. Since both arrays are of int type, then that is the type of pointer you should be using. Also your swap method does not make much sense, again you are swapping pointers not data.
|
|
|
|
|
Because I would like to swap pointers, not to copy all the data. If I swap 2 arrays of 1k size using pointers is 1000x times faster than copying all the data.
This code do it using void * pointers (but I do not know if it could work in other SO), so the next step is to place it in a function:
int a[3]={100,101,102};
int b[3]={200,201,202};
void *x;int *a1=a,*b1=b;
x=(void *) a1;a1=b1;b1=(int *) x;
cout << "a:"<<a1[0]<<" "<<a1[1]<<" "<<a1[2]<<endl;
cout << "b:"<<b1[0]<<" "<<b1[1]<<" "<<b1[2]<<endl;
I tried this function using C++11 and seems to work:
template<class A>
void swap(A *&a,A *&b)
{
A *x=a;a=b;b=x;
}
void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
int *a1=a,*b1=b;
swap(a1,b1);
cout << "a:"<<a1[0]<<" "<<a1[1]<<" "<<a1[2]<<endl;
cout << "b:"<<b1[0]<<" "<<b1[1]<<" "<<b1[2]<<endl;
}
Unfortunately I cannot call swap using a and b:
swap(a,b)
modified 21-Aug-17 3:52am.
|
|
|
|
|
void *x;int *a1=a,*b1=b;
x=(void *) a1;a1=b1;b1=(int *) x;
That just makes no sense, x should be int* the same as a and b .
|
|
|
|
|
As I am sure this is homework I won't give you the answer, what I will tell you is the problem with the typecast
Using the dereference operator (the *) is like mathematics there is an order to things just like in mathematics.
If I gave you 2 + 3 * 4 and you wanted the add before the multiply you need brackets (2 + 3) * 4 the mult carries higher precedence normally.
Same problem happens in dereference operator when using arrays
double **a;
*a[2] has two possible breakdowns so lets use brackets (*a)[2] or *(a[2])
*a[2] is actually equivalent to the latter *(a[2])
I bet that is not what you were expecting and reading it as
Your code doesn't do anything like what your text comments say because you are missing brackets. You need the dereference completed prior to the array use and (*a)[2] is the correct use for you.
Lets give you a simple code
double myArray[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
double *p = &myArray[0];
double d = p[3];
double **q = &p;
double s = (*q)[3];
double t = *q[2];
Does that last line look familiar?
It is the same if the you are derefencing an object or a class
*a.xyz may naively be intended as (*a).xyz or *(a.xyz)
*a->xyz may naively be intended as (*a)->xyz or *(a->xyz)
So look up and learn the precedence order of dereferencing and if you want the other USE BRACKETS. It's actually good practice to always use brackets if you are dereferencing complex items so you know for sure what the order will be be, you do the same with long chains with mathematics. It's a normal gotcha to not dereference what you think you are on complex items because of dereference precedence order (you can't just read it left to right and expect that order).
In vino veritas
modified 21-Sep-16 1:38am.
|
|
|
|
|
|
Can anyone suggest me, how to implement program that break (.png) image in two equal part?
|
|
|
|
|
|
If you're able to, you really should consider leveraging c++ for this task.
Doing so allows you to use functionality built into windows for well over a decade - GDI+.
GDI+ allows one to read formats windows supports natively, also including methods that facilitate the saving of bitmap images to files on disk.
|
|
|
|
|
He has asked for C and he said nothing about Windows so why bring up Windows/GDI ... You need to remember not everything that is requested is for a PC and often not on Windows.
It's highly possible he doesn't have a C++ compiler available or even as an option for what he is working on. There is a very large embedded industry out there and almost none of those micro-controllers and processors have a C++ compiler available.
Don't make assumptions about what he is doing.
In vino veritas
|
|
|
|
|
Simple. He didn't explicitly say they would be no good. CodProject is, as you're no doubt fully aware, an MS-centric site. It's also not uncommon for new members to be less explicit than would be beneficial.
I don't need to remember anything pal. I'm fully aware that plenty of articles here are for Arduino projects.
No-poop sherlock. I've spent time in it myself.
Go back and read my post, ya dolt. Here, I've saved you the trouble. I even highlighted the important parts you seemed to have made your own assumptions about.
If you're able to, you really should consider leveraging c++ for this task.
The only assumption I made was that all offerings of help made in good faith would be gratefully received - not necessarily useful, but received gratefully all the same.
Go and get a dog up ya
|
|
|
|
|
I have no idea why you feel the rant is necessary .. I have referred it to admin
I found your comment strange for the following reasons, so we are clear
1.) The original PNG library was written in C and is still maintained (20 years so far)
2.) It's dubious there is any advantage on converting PNGLIB to C++ you are just likely to get bugs any C++ compiler can compile PNGLIB anyhow
3.) The OP said nothing about windows
4.) Even if you are on windows the GDI+ does not support PNG because it is licenced .. SEE => PNGLIB.
Even IPicture which support JPG and GIF does not support PNG. So you would still have to write your own GDI+ extension code.
There is no malice intended, I just think your advice was misguided for the above reasons. Now if I am in error on any of those points then please let me know.
In vino veritas
modified 20-Sep-16 6:36am.
|
|
|
|
|
You might use a library, or, as others suggested, GDI+ . The natural way for accessing GDI+ is using C++ . However it exposes also a (not recommended) flat API[^].
|
|
|
|
|
Same comment as above the poster said nothing about being on Windows. Why would you assume he has it available? He asked to split a PNG file with C code that was it.
In vino veritas
|
|
|
|